How to configure CLion IDE for Qt Framework?(如何为 Qt 框架配置 CLion IDE?)
问题描述
如何为 Qt 框架配置 CLion IDE?这个IDE是否兼容Qt,或者还有其他兼容Qt的IDE吗?
How to configure CLion IDE for Qt Framework? Is this IDE compatible with Qt, or are there other IDEs compatible with Qt?
我只是想尝试使用 Qt Creator 以外的东西.
I just want to try to use something else than Qt Creator.
推荐答案
我和你一样绝望,直到我读到 这个 Quora 讨论.它非常适合我!
I was as desperate as you, until I read this Quora discussion. It worked perfectly for me!
总而言之,有两个主要步骤:
To summarize, there are 2 main steps:
首先,CLion 使用 CMake 来编译您的代码.它基于 CMake 配置文件(例如CMakeLists.txt").您必须添加基于 Qt 的 CMake 命令(带有find_package"和target_link_libraries"的行):
Firstly, CLion uses CMake to compile your code. It is based on CMake configuration files (e.g "CMakeLists.txt"). You have to add Qt based CMake commands (the lines with 'find_package' and 'target_link_libraries'):
cmake_minimum_required(VERSION 3.5)
project(myqtproject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
find_package(Qt5Widgets REQUIRED) <-- this line
add_executable(myqtproject ${SOURCE_FILES})
target_link_libraries(myqtproject Qt5::Widgets) <-- this line
其次,CLion 必须使用 Qt 安装的 cmake
二进制文件.为此,请访问:'Preferences' -> 'Build, Execution, Deployment' -> 'CMake' 并在 'CMake options' 中添加 Qt 使用的 CMake 路径,该路径应该在安装 Qt 的目录中.例如,在 OSX 上:
Secondly, CLion has to use the cmake
binary installed by Qt. For that, go to:
'Preferences' -> 'Build, Execution, Deployment' -> 'CMake' and in 'CMake options' append the CMake path that Qt uses, which should be in the directory where Qt is installed. For instance, on OSX:
-DCMAKE_PREFIX_PATH=/Users/edouard/Qt/5.7/clang_64/lib/cmake
您可以通过在 main.cpp
中编写一个小测试脚本来测试一切是否正常:
You can test that everything is working fine, by doing a little test script in main.cpp
:
#include <QApplication>
#include <QDebug>
using namespace std;
int main() {
qDebug() << QT_VERSION_STR;
return 1;
}
应该显示如下内容:
/Users/edouard/Library/Caches/CLion2016.2/cmake/generated/myqtproject-89a4132/89a4132/Debug/untitled
5.7.0
Process finished with exit code 1
更新
我遇到了添加 Qt5 模块(例如 QSql)的问题.您可以通过添加 CMakeLists.txt 来做到这一点:
I was stuck with the problem of adding Qt5 modules (for instance QSql). You can do this by adding in the CMakeLists.txt:
find_package(Qt5Sql REQUIRED)
就在另一个find_package
之后,并在最后一行添加:
just after the other find_package
, and adding in the last line:
target_link_libraries(myqtproject Qt5::Widgets Qt5::Sql)
您可以使用所有其他 Qt5 模块执行此操作.
You can do this with all the other Qt5 modules.
这篇关于如何为 Qt 框架配置 CLion IDE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 Qt 框架配置 CLion IDE?
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01