Qt is a cross-platform C++ framework for creating GUI applications. Qt uses its own build system, qmake, and also supports building with CMake starting from the version Qt4.
Qt是一款创建桌面程序的跨平台的C++框架。qmake是Qt自有的构建系统,从Qt4版本开始,Qt系统开始支持CMake进行构建。
A pure Qmake project can't be imported in CLion directly. However, when converted into CMake, it can be opened and managed as a regular CMake application. You can also create a CMake-based Qt project in CLion using the New Project wizard.
CLion无法直接引入由qmake创建的项目,但是可以打开从qmake转换成cmake的Qt项目。开发人员也可以通过CLion的工程向导创建基于CMake的Qt工程。
For CMake version 3.0 and newer, Qt ships the modules that let CMake find and use Qt4 and Qt5 libraries. Take the following steps to configure CMakeLists.txt for your Qt project.
对于CMake 3.0及更新版本,CMake 查找Qt模块并使用 Qt4 和 Qt5 库。请按以下步骤为您的 Qt 项目配置 CMakeLists.txt。
Qt项目的CMakeList.txt文件
find_package
command to locate the required libraries and header files. For example:添加find_package命令定位查找所需的库文件和头文件,例如:
find_package(Qt5Widgets REQUIRED)
Then, use target_link_libraries
to make your target dependent on these libraries and headers:
随后,使用target_link_libraries
命令将你的目标绑定查找到的库文件和头文件。
target_link_libraries(helloworld Qt5::Widgets)
find_package
to perform successfully, CMake should be instructed on where to find the Qt installation.当find_package命令运行成功后,接下来给CMake配置Qt的安装配置。
One of the ways to do this is by setting the CMAKE_PREFIX_PATH
variable. You can either pass it via -D
in the CMake settings dialog or via the set
command before find_package
.
一种方式是通过给CMake设定CMAKE_PREFIX_PATH
变量。这种设定方式既可以通过在CMake的设置对话框中传入“-D”或者在find_package命令之前通过set命令设置。
For example, in the case of MinGW on Windows:
例如,在Windows系统中设置MinGW:
set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.14.0\\5.14.0\\mingw73_32\\")
或者
set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.14.0\\5.14.0\\mingw73_32\\lib\\cmake\\")
如果项目中使用到MOC、UIC或者RCC,请添加以下命令启动相应的编译器:
- set(CMAKE_AUTOMOC ON)
- set(CMAKE_AUTOUIC ON)
- set(CMAKE_AUTORCC ON)
add_executable()
command along with your .cpp sources:在add_executable()命令列出你的“.cpp”文件、“.ui”文件和“.qrc”文件。
- add_executable(
- helloworld
- main.cpp mainwindow.cpp
- application.qrc
- )
Below you can find the full CMakeLists.txt script for a simple "Hello, world" application:
下面列出了“Hello, world”程序所需的CMakeList.txt脚本:
- cmake_minimum_required(VERSION 3.10)
- project(Qt-CMake-HelloWorld)
-
- set(CMAKE_CXX_STANDARD 17)
- set(CMAKE_INCLUDE_CURRENT_DIR ON)
- set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.14.0\\5.14.0\\mingw73_32\\")
-
- find_package(Qt5Widgets REQUIRED)
-
- set(CMAKE_AUTOMOC ON)
- set(CMAKE_AUTOUIC ON)
- set(CMAKE_AUTORCC ON)
-
- add_executable(helloworld main.cpp mainwindow.cpp application.qrc)
- target_link_libraries(helloworld Qt5::Widgets)
https://www.jetbrains.com/help/clion/qt-tutorial.html