Clion 搭建Qt projects

59 篇文章 1 订阅
46 篇文章 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
    评论
CLion 是一款由 JetBrains 公司开发的集成开发环境(IDE),主要用于 C/C++ 开。它与 Qt 的集成并不直接,但是你可以在 CLion 中使用 Qt 进行开发。 在 CLion 中使用 Qt 进行开发,你需要进行以下操作: 1. 安装 Qt:首先,你需要安装 Qt 开发框架。你可以从 Qt 的官方网站下载并安装 Qt。安装完成后,记住你的 Qt 安装路径。 2. 配置 CLion:打开 CLion,进入 "Settings"(Windows 和 Linux 上是 "File" -> "Settings",macOS 上是 "CLion" -> "Preferences"),然后选择 "Build, Execution, Deployment" -> "CMake"。 - 在 "CMake" 选项卡中,点击 "Add" 添加一个新的 CMake 配置文件。 - 在弹出的窗口中,将 "Name" 设置为 "Qt"(或者任何你喜欢的名称),然后将 "Build type" 设置为 "Debug" 或 "Release"。 - 在 "CMake options" 字段中,添加以下内容: ``` -DCMAKE_PREFIX_PATH=<Qt 安装路径>/clang_64/lib/cmake ``` 这里需要将 `<Qt 安装路径>` 替换为你实际的 Qt 安装路径。 3. 创建项目:在 CLion 中创建一个新的 C++ 项目或打开一个已有的项目。确保你的项目正确配置了 CMake。 4. 配置项目:在项目的根目录下,创建一个名为 `CMakeLists.txt` 的文件。在这个文件中,你需要添加 Qt 相关的配置。以下是一个示例的 `CMakeLists.txt` 文件: ```cmake cmake_minimum_required(VERSION 3.20) project(QtProject) set(CMAKE_CXX_STANDARD 17) find_package(Qt5 COMPONENTS REQUIRED Core Widgets) add_executable(QtProject main.cpp) target_link_libraries(QtProject PRIVATE Qt5::Core Qt5::Widgets) ``` 这个示例中使用了 Qt 的 Core 和 Widgets 组件,你可以根据自己的需要添加其他组件。 5. 开始开发:现在你就可以在 CLion 中使用 Qt 进行开发了。你可以创建 Qt 的窗口、使用 Qt 的库、连接信号和槽等。 这就是在 CLion 中使用 Qt 进行开发的基本步骤。希望对你有所帮助!如果你有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值