Clion 搭建Qt projects

57 篇文章 1 订阅
44 篇文章 0 订阅

Qt projects

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工程。

CMake-based Qt projects

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。

CMakeLists.txt for a Qt project

Qt项目的CMakeList.txt文件

  • Add the 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)
  • For 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\\")
  • If your project uses MOC, UIC, or RCC, add the following lines to enable automatic invocation of the corresponding compilers:

    如果项目中使用到MOC、UIC或者RCC,请添加以下命令启动相应的编译器:

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
  • List all the .ui and .qrc files in the 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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c 环境配置 colin 主要包括三个步骤:安装编译器、配置环境变量和测试编译器。 首先,安装编译器。在安装 c 环境之前,我们需要先下载对应的编译器软件。常用的 c 编译器有 gcc、clang、MSVC 等。根据个人需求选择适合自己的编译器,并下载对应的安装包。然后,按照安装向导进行安装即可。安装完成后,我们就拥有了一个可用的 c 编译器。 接下来,配置环境变量。环境变量的配置可以使得我们在任意目录下都能够直接使用 c 编译器。首先,找到我们安装的编译器安装目录。然后,将该目录添加到系统的环境变量中。具体的配置步骤可以根据操作系统的不同而有所差异,但一般都是在控制面板或者系统设置中找到“环境变量”选项,然后添加编译器安装目录到系统的“PATH”变量中。配置完成后,我们就可以在任意目录下打开命令行终端,输入编译器命令来编译和运行 c 程序。 最后,测试编译器。经过以上两个步骤的配置,我们就可以测试我们的 c 编译器是否正常工作了。在任意目录下新建一个文本文件,将其后缀改为“.c”,比如“test.c”。然后,用任意文本编辑器打开该文件,输入一段简单的 c 代码,比如“#include <stdio.h> int main(){ printf("Hello, world!"); return 0; }”。保存文件后,回到命令行终端,进入该文件所在的目录,输入编译命令,如“gcc test.c -o test”,然后按回车键进行编译。如果没有出现错误信息,说明我们的 c 编译器已经成功配置。 综上所述,通过安装编译器、配置环境变量和测试编译器三个步骤,我们可以完成 c 环境的配置,从而能够顺利编译和运行 c 程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值