在Mac OS上使用Visual Studio Code创建C++ Qt的Hello World应用

2024-06-23 17:44

本文主要是介绍在Mac OS上使用Visual Studio Code创建C++ Qt的Hello World应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

引言
Qt是一个跨平台的应用程序和用户界面框架,而Visual Studio Code是一个功能强大的编辑器,两者结合可以极大地提升开发效率。本文将指导你在Mac OS上使用Visual Studio Code创建一个简单的Qt 'Hello World'窗口应用。

环境准备

  • 确保你的MacBook OS运行最新的操作系统。
  • 安装Homebrew,Mac OS的包管理器。
  • 通过Homebrew安装Qt:brew install qt
  • 安装Visual Studio Code。
  • 在Visual Studio Code中安装C++扩展。

创建Qt项目

1、使用Qt的qmake工具创建项目:

qmake -project "QT += widgets" -o .project
qmake

2、创建mainwindow.hmainwindow.cpp文件,实现窗口和主函数。

编写代码

  • mainwindow.h定义了窗口类。
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H#include <QMainWindow>class MainWindow : public QMainWindow
    {Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();
    };#endif // MAINWINDOW_H
  • mainwindow.cpp实现了窗口的构造和析构。
    #include "mainwindow.h"
    #include <QApplication>
    #include <QLabel>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {setWindowTitle("Hello World");QLabel *label = new QLabel("Hello World", this);label->setGeometry(50, 50, 200, 50); // 位置和大小可以根据需要调整
    }MainWindow::~MainWindow()
    {// 析构函数
    }
  • main.cpp是程序的入口点,创建并显示MainWindow
    #include <QApplication>
    #include "mainwindow.h"int main(int argc, char *argv[])
    {QApplication app(argc, argv);MainWindow mainWindow;mainWindow.show();return app.exec();
    }

构建项目

  1. 确保.pro文件正确配置了项目设置和文件列表。
    QT       += core gui widgetsTARGET = hello
    TEMPLATE = appSOURCES += main.cpp \mainwindow.cppHEADERS += mainwindow.h
  2. 使用qmake生成Makefile
    qmake project.pro

  3. 使用make命令构建项目:
    make

运行应用
运行生成的可执行文件:

./hello.app/Contents/MacOS/hello
 

错误处理
如果在构建或运行过程中遇到问题,请检查:

  • 所有文件是否在正确的位置。
  • .pro文件是否包含了所有必要的源文件和头文件。
  • Qt和编译器的版本是否兼容。

总结
通过本文,你学会了如何在Mac OS上使用Visual Studio Code和Qt创建一个基本的'Hello World'窗口应用。这只是一个开始,Qt的强大功能等待着你去探索。

附加资源

  • Qt官方文档
  • Visual Studio Code官方教程

douxiaobo@192 helloworld_qt % code .
douxiaobo@192 helloworld_qt % qmake -project "QT += widgets" -o .project
douxiaobo@192 helloworld_qt % qmake
Usage: qmake [mode] [options] [files]QMake has two modes, one mode for generating project files based on
some heuristics, and the other for generating makefiles. Normally you
shouldn't need to specify a mode, as makefile generation is the default
mode for qmake, but you may use this to test qmake on an existing projectMode:-project       Put qmake into project file generation modeIn this mode qmake interprets [files] as files tobe added to the .pro file. By default, all files withknown source extensions are added.Note: The created .pro file probably will need to be edited. For example add the QT variable to specify what modules are required.-makefile      Put qmake into makefile generation mode (default)In this mode qmake interprets files as project files tobe processed, if skipped qmake will try to find a projectfile in your current working directoryWarnings Options:-Wnone         Turn off all warnings; specific ones may be re-enabled bylater -W options-Wall          Turn on all warnings-Wparser       Turn on parser warnings-Wlogic        Turn on logic warnings (on by default)-Wdeprecated   Turn on deprecation warnings (on by default)Options:* You can place any variable assignment in options and it will be ** processed as if it was in [files]. These assignments will be    ** processed before [files] by default.                            *-o file        Write output to file-d             Increase debug level-t templ       Overrides TEMPLATE as templ-tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value-help          This help-v             Version information-early         All subsequent variable assignments will beparsed right before default_pre.prf-before        All subsequent variable assignments will beparsed right before [files] (the default)-after         All subsequent variable assignments will beparsed after [files]-late          All subsequent variable assignments will beparsed right after default_post.prf-norecursive   Don't do a recursive search-recursive     Do a recursive search-set <prop> <value> Set persistent property-unset <prop>  Unset persistent property-query <prop>  Query persistent property. Show all if <prop> is empty.-qtconf file   Use file instead of looking for qt6.conf, then qt.conf-cache file    Use file as cache           [makefile mode only]-spec spec     Use spec as QMAKESPEC       [makefile mode only]-nocache       Don't use a cache file      [makefile mode only]-nodepend      Don't generate dependencies [makefile mode only]-nomoc         Don't generate moc targets  [makefile mode only]-nopwd         Don't look for files in pwd [project mode only]
douxiaobo@192 helloworld_qt % make
make: *** No targets specified and no makefile found.  Stop.
douxiaobo@192 helloworld_qt % make project.pro
make: Nothing to be done for `project.pro'.
douxiaobo@192 helloworld_qt % qmake project.pro
Info: creating stash file /Users/douxiaobo/Documents/Practice in Coding/C++/helloworld_qt/.qmake.stash
WARNING: Failure to find: mainwindow.h
douxiaobo@192 helloworld_qt % qmake project.pro
WARNING: Failure to find: mainwindow.h
douxiaobo@192 helloworld_qt % qmake project.pro
douxiaobo@192 helloworld_qt % make
/Library/Developer/CommandLineTools/usr/bin/clang++ -c -pipe -stdlib=libc++ -O2 -std=gnu++1z  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wall -Wextra -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/opt/homebrew/lib/QtWidgets.framework/Headers -I/opt/homebrew/lib/QtGui.framework/Headers -I/opt/homebrew/lib/QtCore.framework/Headers -I. -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/opt/homebrew/share/qt/mkspecs/macx-clang -F/opt/homebrew/lib -o main.o main.cpp
/Library/Developer/CommandLineTools/usr/bin/clang++ -c -pipe -stdlib=libc++ -O2 -std=gnu++1z  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wall -Wextra -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/opt/homebrew/lib/QtWidgets.framework/Headers -I/opt/homebrew/lib/QtGui.framework/Headers -I/opt/homebrew/lib/QtCore.framework/Headers -I. -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/opt/homebrew/share/qt/mkspecs/macx-clang -F/opt/homebrew/lib -o mainwindow.o mainwindow.cpp
/Library/Developer/CommandLineTools/usr/bin/clang++ -pipe -stdlib=libc++ -O2 -std=gnu++1z  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wall -Wextra -dM -E -o moc_predefs.h /opt/homebrew/share/qt/mkspecs/features/data/dummy.cpp
/opt/homebrew/share/qt/libexec/moc -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB --include '/Users/douxiaobo/Documents/Practice in Coding/C++/helloworld_qt/moc_predefs.h' -I/opt/homebrew/share/qt/mkspecs/macx-clang -I'/Users/douxiaobo/Documents/Practice in Coding/C++/helloworld_qt' -I/opt/homebrew/lib/QtWidgets.framework/Headers -I/opt/homebrew/lib/QtGui.framework/Headers -I/opt/homebrew/lib/QtCore.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 -I/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/usr/include -F/opt/homebrew/lib mainwindow.h -o moc_mainwindow.cpp
/Library/Developer/CommandLineTools/usr/bin/clang++ -c -pipe -stdlib=libc++ -O2 -std=gnu++1z  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wall -Wextra -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/opt/homebrew/lib/QtWidgets.framework/Headers -I/opt/homebrew/lib/QtGui.framework/Headers -I/opt/homebrew/lib/QtCore.framework/Headers -I. -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/opt/homebrew/share/qt/mkspecs/macx-clang -F/opt/homebrew/lib -o moc_mainwindow.o moc_mainwindow.cpp
/Library/Developer/CommandLineTools/usr/bin/clang++ -stdlib=libc++ -headerpad_max_install_names  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wl,-rpath,@executable_path/../Frameworks -Wl,-rpath,/opt/homebrew/lib -o hello.app/Contents/MacOS/hello  main.o mainwindow.o moc_mainwindow.o   -F/opt/homebrew/lib -framework QtWidgets -framework QtGui -framework AppKit -framework ImageIO -framework Metal -framework QtCore -framework IOKit -framework DiskArbitration -framework AGL -framework OpenGL   
douxiaobo@192 helloworld_qt % ./hello.app/Contents/MacOS/hello
douxiaobo@192 helloworld_qt % 

project.pro

QT += widgetsSOURCES += mainwindow.cpp

mainwindow.cpp

#include <QApplication>
#include <QMainWindow>int main(int argc, char *argv[]) {QApplication app(argc, argv);QMainWindow window;window.setWindowTitle("Hello World");window.show();return app.exec();
}

运行结果成功了。

命令行如下:

douxiaobo@192 helloworld_qt % qmake project.pro
douxiaobo@192 helloworld_qt % make
/Library/Developer/CommandLineTools/usr/bin/clang++ -c -pipe -stdlib=libc++ -O2 -std=gnu++1z  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wall -Wextra -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/opt/homebrew/lib/QtWidgets.framework/Headers -I/opt/homebrew/lib/QtGui.framework/Headers -I/opt/homebrew/lib/QtCore.framework/Headers -I. -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AGL.framework/Headers -I/opt/homebrew/share/qt/mkspecs/macx-clang -F/opt/homebrew/lib -o mainwindow.o mainwindow.cpp
/Library/Developer/CommandLineTools/usr/bin/clang++ -stdlib=libc++ -headerpad_max_install_names  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=14.0 -Wl,-rpath,@executable_path/../Frameworks -Wl,-rpath,/opt/homebrew/lib -o project.app/Contents/MacOS/project  mainwindow.o   -F/opt/homebrew/lib -framework QtWidgets -framework QtGui -framework AppKit -framework ImageIO -framework Metal -framework QtCore -framework IOKit -framework DiskArbitration -framework AGL -framework OpenGL   
douxiaobo@192 helloworld_qt % ./project.app/Contents/MacOS/project
douxiaobo@192 helloworld_qt % 

这篇关于在Mac OS上使用Visual Studio Code创建C++ Qt的Hello World应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1087841

相关文章

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态