Qt 概述
- 概念
Qt 是一个跨平台的 C++ 图形用户界面应用程序框架
常见的 C++ GUI: Qt 和 MFC
- 跨平台
-
Windows
-
Linux
-
MacOS
-
嵌入式平台
- 版本
包括商业版和开源免费版
- 案例
-
Linux 桌面环境 KDE
-
WPS Office
Qt 安装
下载地址:
-
https://download.qt.io/archive/qt/
-
https://www.qt.io/download-open-source
构建 Qt 项目
参考: https://doc.qt.io/qt-5/cmake-get-started.html
新建一个目录 Begin, 在该目录下新建 CMakeLists.txt, main.cpp 以及 build 空目录
Begin/
├── build
├── CMakeLists.txt
└── main.cpp
1 directory, 2 files
- CMakeLists.txt 的内容:
cmake_minimum_required(VERSION 3.10)
project(Begin)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_PREFIX_PATH /opt/Qt/5.15.2/gcc_64/lib/cmake)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
add_executable(00-Demo main.cpp)
target_link_libraries(00-Demo Qt5::Widgets)
Qt 5.15 以后的版本, 模块库也可以写为 Qt::Widgets, 以便兼容 Qt6
- main.cpp 的内容:
#include <QApplication>
#include <QWidget>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
return app.exec(); // 主事件循环
}
编译 Qt 项目
进入 Begin/build 目录
$ cd Begin/build/
$ cmake .. && make
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jianghuixin/CLion/Qt5/Begin/build
[ 25%] Automatic MOC and UIC for target 00-Demo
[ 25%] Built target 00-Demo_autogen
[ 50%] Building CXX object CMakeFiles/00-Demo.dir/00-Demo_autogen/mocs_compilation.cpp.o
[ 75%] Building CXX object CMakeFiles/00-Demo.dir/main.cpp.o
[100%] Linking CXX executable 00-Demo
[100%] Built target 00-Demo
在 build 目录生成了 00-Demo 可执行程序, 直接运行
$ ./00-Demo