欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

快速上手Qt编程:6.x版本开发环境设置与第一个Hello World程序

最编程 2024-08-04 09:41:15
...
环境
OS: macOS 11.6
Qt版本: 6.2.2

一、下载Qt在线安装器,使用在线安装器来安装你需要的Qt功能模块

Qt包括收费的部分和开源的部分,对于普通用户下载开源部分即可。
下载地址: https://www.qt.io/download
选择开源部分,如下:

Qt_opensource.png

点击 Go open source 按钮,跳转到下面的页面(向下滑动才能看到 Download the Qt Online Installer 按钮)

qt-online-installer.png

点击 Download the Qt Online Installer 按钮,跳转到具体的下载页面 (这个页面可以选择你操作系统对应的版本,一般跳转到这个页面时网站后端已经根据你当前的操作系统给你默认选择了相应的版本,如果你要下载其他版本,可以自行选择)
qt-select-os.png

点击 Download 按钮,开始下载 (注意这里下载的是在线安装器,而不是Qt的Framework或者开发工具)。

下载完安装器后你就可以运行安装器,安装你需要的模块了。
由于 安装器安装Qt模块时需要xcode的支持(xcode太占空间,被我删除了!!),因此我就没用 在线安装器来安装Qt了。

二、使用 brew 命令来安装Qt

下面是我使用的方法:

使用 Homebrew(依赖库管理神器)来安装Qt
# 这种方式安装的是Qt的Framework相关的东西,没有开发工具。
$ brew install qt # 使用brew安装Qt

# 安装之前你可以使用下列命令获取相关信息: 
$ brew list  # 列出本机已安装的模块
$ brew search <FORMULE> # 搜索关键字所指定的模块
$ brew info <FORMULE> # 查看指定的模块的信息



搜索、查看 \color{blue}{qt} 的相关信息

$ brew search qt    # 输出如下: 
==> Formulae
fastq-tools         pyqt@5              qt-unixodbc         seqtk
neovim-qt           qt ✔                qt@5                sqtop
poppler-qt5         qt-libiodbc         qtads               wolfmqtt
pyqt                qt-mariadb          qtfaststart         q
pyqt-3d             qt-mysql            qthreads
pyqt-builder        qt-percona-server   qtkeychain
pyqt-networkauth    qt-postgresql       qwt-qt5

==> Casks
homebrew/cask-versions/qt-creator-dev

# 关于 Formulae 和 Cask 的区别:
# Formulae 一般是提供功能的库(例如这个Formula的安装目录是<DIR>,
#     则此目录下有:incude头文件目录、lib静态/动态库目录,
#     还有包涵执行命令的bin目录),
#     如:python、dart、gcc、binutils ...... 等等,这些都是属于Formula类型。
# Casks 是单独运行的应用程序(一般是带GUI界面的),如VLC、qt-creator 等等

# man brew 输出的信息中有关于Formula和Cask的解释,如下:
# formula
#       Homebrew package definition built from upstream sources
# cask
#       Homebrew package definition that installs macOS native applications


# 查看库qt的相关信息
$ brew info qt    # 输出如下:
qt: stable 6.2.2 (bottled), HEAD
Cross-platform application and UI framework
https://www.qt.io/
Not installed
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/qt.rb
License: GFDL-1.3-only and GPL-2.0-only and GPL-3.0-only and LGPL-2.1-only and LGPL-3.0-only
==> Dependencies
Build: cmake ✘, ninja ✘, node ✘, pkg-config ✔
Required: assimp ✘, brotli ✔, dbus ✘, double-conversion ✘, freetype ✔, glib ✘, hunspell ✘, icu4c ✔, jasper ✘, jpeg ✔, libb2 ✘, libpng ✔, libtiff ✔, md4c ✘, pcre2 ✔, python@3.9 ✔, sqlite ✔, webp ✔, zstd ✔
==> Requirements
Build: Xcode ✘
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 35,719 (30 days), 141,749 (90 days), 685,060 (365 days)
install-on-request: 17,610 (30 days), 70,753 (90 days), 273,984 (365 days)
build-error: 74 (30 days)

# Dependencies 列出了qt所需要的依赖



qt的依赖库介绍 (打勾的表示已经安装过了,已经安装过的依赖库我就省略了,感兴趣的可以自己百度搜索一下,brew info formula-name 也可以查看相关信息):

ninja  类似与cmake的构建工具。
assimp  一个非常流行的3D模型导入库 (OpenGL中使用)。
glib  linux下一个常用的GUI库, 也可以快平台。

hunspell  Hunspell是一个拼写检查器,OpenOffice 和 Firefox 以及 Thunderbird 默认的都是使用这个拼写检查器。
    支持 C/C++、Java、Delphi、Ruby、Perl、Python 等编程语言。
    http://hunspell.github.io/
    https://github.com/hunspell/hunspell

double-conversion  这是一个双精度浮点数转换相关的库
    https://github.com/google/double-conversion

jasper  图片处理库 (Library for manipulating JPEG-2000 images)
        https://ece.engr.uvic.ca/~frodo/jasper/

libb2  一个hash算法库

md4c  C Markdown parser (C语言markdown解析器)
      https://github.com/mity/md4c

dbus 一个消息总线系统: https://www.freedesktop.org/wiki/Software/dbus/


libb2

GitHub地址:https://github.com/BLAKE2/libb2
描述:C library providing BLAKE2b, BLAKE2s, BLAKE2bp, BLAKE2sp
assimp

官网: http://assimp.org
GitHub地址: https://github.com/assimp/assimp
描述:assimp是一个非常流行的3D模型导入库,它是 Open Asset Import Library(开放的资产导入库)的缩写。

参考: https://learnopengl-cn.github.io/03%20Model%20Loading/01%20Assimp/
glib

GitHub地址:https://gitlab.gnome.org/GNOME/glib
GTK官网:https://www.gtk.org/
描述:glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性。
glib 是 GTK+ 库和 GNOME 的基础。glib可以在多个平台下使用,比如Linux、Unix、Windows等。glib为许多标准的、常用的C语言结构提供了相应的替代物。
dbus依赖于xmlto
xmlto官网:https://pagure.io/xmlto/
glib依赖meson, meson是一个开源的构建系统.
meson官网:https://mesonbuild.com/


三、编写第一个Qt程序

// -std=c++11 在 Qt6.2.2 上会报错
// 注意: Qt 6.2.2 需要 C++17 !!!
//
// 这个编译命令会报错:
// g++ -I/usr/local/Cellar/qt/6.2.2/include -std=c++11 -F/usr/local/Cellar/qt/6.2.2/lib -framework QtCore -framework QtWidgets -o helloworld demo1.cpp

// 正确编译命令:
// g++ -I/usr/local/Cellar/qt/6.2.2/include -std=c++17 -F/usr/local/Cellar/qt/6.2.2/lib -framework QtCore -framework QtWidgets -o helloworld demo1.cpp
// 参数说明: -I 指定头文件的include目录, -F 添加framework搜索路径

// gcc/g++ 命令参数:
// -I <dir>     Add directory to include search path
// -L <dir>     Add directory to library search path
// -F <value>   Add directory to framework include search path

// 参考: https://www.jianshu.com/p/648cfb943f2a

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>

int main(int argc, char** argv) {
    QApplication app(argc, argv);
    QLabel label("Hello world !");
    label.show();
    return app.exec();
}
# 编译程序:
$ g++ -std=c++17 \
-I/usr/local/Cellar/qt/6.2.2/include \
-F/usr/local/Cellar/qt/6.2.2/lib \
-framework QtCore \
-framework QtWidgets \
-o helloworld demo1.cpp

# g++ 命令参数说明:
# -std=c++11   指定c++的版本
# -I <dir>      Add directory to include search path
# -F <value>    Add directory to framework include search path
# -framework  指定framework
# -o          指定输出文件名称



下面编写一个界面稍微复杂一点的程序:

/**
编译命令, 如下:
$ g++ -std=c++17 \
-I/usr/local/Cellar/qt/6.2.2/include \
-F/usr/local/Cellar/qt/6.2.2/lib \
-framework QtCore \
-framework QtWidgets \
-o demo3 demo3.cpp
*/


#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QWidget>
#include <QtWidgets/QLabel>

int main(int argc,char** argv) {

    QApplication app(argc,argv);
    QLabel *infoLabel = new QLabel;
    QLabel *cmdLabel = new QLabel;
    QLineEdit *cmdLineEdit = new QLineEdit;

    QPushButton *submitButton = new  QPushButton;
    QPushButton *cancelButton = new QPushButton;
    QPushButton *browseButton = new QPushButton;
    
    infoLabel->setText("Please input command");
    cmdLabel->setText("Open:");
    cmdLineEdit->clear();

    submitButton->setText("Submit");
    cancelButton->setText("Cancel");
    browseButton->setText("Browse");

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(submitButton);
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addWidget(browseButton);

    QHBoxLayout *cmdLayout = new QHBoxLayout;
    cmdLayout->addWidget(cmdLabel);
    cmdLayout->addWidget(cmdLineEdit);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(infoLabel);
    mainLayout->addLayout(cmdLayout);
    mainLayout->addLayout(buttonLayout);

    QWidget *window = new QWidget;
    window->setLayout(mainLayout);
    window->setWindowTitle("cmd");
    window->show();
    
    return app.exec();

}
编译上面的程序,得到可执行程序demo3:
g++ -std=c++17 \
-I/usr/local/Cellar/qt/6.2.2/include \
-F/usr/local/Cellar/qt/6.2.2/lib \
-framework QtCore \
-framework QtWidgets \
-o demo3 demo3.cpp

执行程序: ./demo3
界面如下:

GUI.png



参考:
References:
https://www.jianshu.com/p/648cfb943f2a
https://www.dazhuanlan.com/shanghu/topics/1052500
https://blog.****.net/ganhuanghuang/article/details/39276629