ubuntu 使用 CMake 构建 Qt5 项目
创始人
2024-05-31 11:53:25
0

Qt 概述

  1. 概念

Qt 是一个跨平台的 C++ 图形用户界面应用程序框架

常见的 C++ GUI: Qt 和 MFC

  1. 跨平台
  • Windows

  • Linux

  • MacOS

  • 嵌入式平台

  1. 版本

包括商业版和开源免费版

  1. 案例
  • 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 
#include 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

00-Demo

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...