如何为Qt Framework配置CLion IDE? [英] How to configure CLion IDE for Qt Framework?

查看:308
本文介绍了如何为Qt Framework配置CLion IDE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为Qt Framework配置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必须使用 cmake 由Qt安装的二进制文件。为此,请转到:
'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

#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

UPDATE

我陷入了添加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 Framework配置CLion IDE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆