• Clion 搭建Qt projects


    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,请添加以下命令启动相应的编译器:

    1. set(CMAKE_AUTOMOC ON)
    2. set(CMAKE_AUTOUIC ON)
    3. 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”文件。

    1. add_executable(
    2. helloworld
    3. main.cpp mainwindow.cpp
    4. application.qrc
    5. )

    Below you can find the full CMakeLists.txt script for a simple "Hello, world" application:

    下面列出了“Hello, world”程序所需的CMakeList.txt脚本:

    1. cmake_minimum_required(VERSION 3.10)
    2. project(Qt-CMake-HelloWorld)
    3. set(CMAKE_CXX_STANDARD 17)
    4. set(CMAKE_INCLUDE_CURRENT_DIR ON)
    5. set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.14.0\\5.14.0\\mingw73_32\\")
    6. find_package(Qt5Widgets REQUIRED)
    7. set(CMAKE_AUTOMOC ON)
    8. set(CMAKE_AUTOUIC ON)
    9. set(CMAKE_AUTORCC ON)
    10. add_executable(helloworld main.cpp mainwindow.cpp application.qrc)
    11. target_link_libraries(helloworld Qt5::Widgets)

    https://www.jetbrains.com/help/clion/qt-tutorial.html


  • 相关阅读:
    【Linux】【进程控制】
    终于有人把“Linux云计算路线”整理出来了,收藏起来,随时查看
    使用卷积神经网络训练手写数字识别模型(CNN)
    Python 实现Excel自动化办公(上)
    自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
    Ubuntu处理依赖问题
    力扣题49给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
    MBA-day26 数的概念与性质
    【Node.js】Windows环境安装配置NVM和Node.js
    快慢指针技巧
  • 原文地址:https://blog.csdn.net/weiweiqiao/article/details/132025792