在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++的模板(八):子系统

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。 但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

C/C++的编译和链接过程

目录 从源文件生成可执行文件(书中第2章) 1.Preprocessing预处理——预处理器cpp 2.Compilation编译——编译器cll ps:vs中优化选项设置 3.Assembly汇编——汇编器as ps:vs中汇编输出文件设置 4.Linking链接——链接器ld 符号 模块,库 链接过程——链接器 链接过程 1.简单链接的例子 2.链接过程 3.地址和

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

Mac excel 同时冻结首行和首列

1. 选择B2窗格 2. 选择视图 3. 选择冻结窗格 最后首行和首列的分割线加粗了就表示成功了

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使