C++ 如何为 Qt 框架配置 CLion IDE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30235175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure CLion IDE for Qt Framework?
提问by 123qwe
How to configure CLion IDE for Qt Framework? Is this IDE compatible with Qt, or are there other IDEs compatible with Qt?
如何为 Qt 框架配置 CLion IDE?这个 IDE 是否与 Qt 兼容,或者是否有其他与 Qt 兼容的 IDE?
I just want to try to use something else than Qt Creator.
我只是想尝试使用 Qt Creator 以外的东西。
回答by Edouard Berthe
I was as desperate as you, until I read this Quora discussion. It worked perfectly for me!
我和你一样绝望,直到我读到这个 Quora 讨论。它非常适合我!
To summarize, there are 2 main steps:
总结一下,有两个主要步骤:
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'):
首先,CLion 使用 CMake 来编译您的代码。它基于 CMake 配置文件(例如“CMakeLists.txt”)。您必须添加基于 Qt 的 CMake 命令(带有“find_package”和“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
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:
其次,CLion 必须使用cmake
Qt 安装的二进制文件。为此,请转到:“首选项”->“构建、执行、部署”->“CMake”并在“CMake 选项”中添加 Qt 使用的 CMake 路径,该路径应位于安装 Qt 的目录中。例如,在 OSX 上:
-DCMAKE_PREFIX_PATH=/Users/edouard/Qt/5.7/clang_64/lib/cmake
You can test that everything is working fine, by doing a little test script in main.cpp
:
您可以通过在 中执行一个小测试脚本来测试一切是否正常main.cpp
:
#include <QApplication>
#include <QDebug>
using namespace std;
int main() {
qDebug() << QT_VERSION_STR;
return 1;
}
Which should display something like:
应该显示如下内容:
/Users/edouard/Library/Caches/CLion2016.2/cmake/generated/myqtproject-89a4132/89a4132/Debug/untitled
5.7.0
Process finished with exit code 1
UPDATE
更新
I was stuck with the problem of adding Qt5 modules (for instance QSql). You can do this by adding in the CMakeLists.txt:
我被添加 Qt5 模块(例如 QSql)的问题所困扰。您可以通过添加 CMakeLists.txt 来做到这一点:
find_package(Qt5Sql REQUIRED)
just after the other find_package
, and adding in the last line:
紧跟在 other 之后find_package
,并在最后一行添加:
target_link_libraries(myqtproject Qt5::Widgets Qt5::Sql)
You can do this with all the other Qt5 modules.
您可以使用所有其他 Qt5 模块执行此操作。
回答by AshkanVZ
This approach is one of the simplest way to be used for the newest Qtversion.
这种方法是用于最新Qt版本的最简单方法之一。
Qt: 5.10.1
CLion: 2018.1.2
GDB: 8.1
Project setup
项目设置
In CLion:
在CLion 中:
- Create a C++ Executable/Libraryproject
- Use this sample "CMakeLists.txt" for common console/gui projects that uses: QtCore, QtWidgetsand QtQuick
- 创建C++ 可执行文件/库项目
- 将此示例“ CMakeLists.txt”用于使用以下内容的常见控制台/gui 项目:QtCore、QtWidgets和QtQuick
CMakeLists.txt:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/cmake")
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Quick REQUIRED)
add_executable(PROJECT_NAME main.cpp MainWindow.cpp MainWindow.h qml.qrc)
target_link_libraries(PROJECT_NAME Qt5::Core)
target_link_libraries(PROJECT_NAME Qt5::Widgets)
target_link_libraries(PROJECT_NAME Qt5::Quick)
Resource files (.qrc) should be added to add_executable list, in order for moc to be able to run its procedure on the resource and the internal file like qmls, texts, ... be accessible.
qml files should be included in a qrc file and load using QQmlApplicationEngineat runtime
资源文件 (.qrc) 应该添加到 add_executable 列表中,以便 moc 能够在资源和内部文件(如 qmls、texts...)上运行其过程。
qml 文件应包含在 qrc 文件中,并在运行时使用QQmlApplicationEngine加载
Debuger:
调试器:
In order to has a human-readable view in debug sessions from Qt types, A new GDB must be installed on the system and pretty printers must be available:
为了在 Qt 类型的调试会话中具有人类可读的视图,必须在系统上安装新的 GDB 并且必须有漂亮的打印机:
Pretty printers for Qt5:
Qt5 的漂亮打印机:
1- LekensteynQt5 Pretty Printers(Working):
1- Lekensteyn Qt5 漂亮的打印机(工作):
Address: https://github.com/Lekensteyn/qt5printers
地址:https: //github.com/Lekensteyn/qt5printers
Setup: ~/.gdbinit
设置:~/.gdbinit
python
import sys, os.path
sys.path.insert(0, os.path.expanduser('~/.gdb'))
import qt5printers
qt5printers.register_printers(gdb.current_objfile())
end
2- Official (not working!!!):
2-官方(不工作!!!):
"PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/share/qtcreator/debugger/"
Setup: ~/.gdbinit
设置:~/.gdbinit
As stated in included readme file (but not working!):
如包含的自述文件所述(但不起作用!):
python sys.path.insert(1, '<path/to/qtcreator>/share/qtcreator/debugger/')
python from gdbbridge import *
External tools
外部工具
In "File -> Settings -> Tools -> External Tools", add 4 external tools:
在“文件->设置->工具->外部工具”中,添加4个外部工具:
Qt Creator:
Qt 创建者:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/bin/qtcreator"
Arguments: $FilePath$
UI Designer:
界面设计师:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/designer")
Arguments: $FilePath$
LUpdate:
L更新:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/lupdate")
Arguments: $FilePath$ -ts $FileNameWithoutExtension$.ts
Linguist:
语言学家:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/linguist")
Arguments: $FilePath$
Now you can right click these file types and under the external tool:
现在您可以右键单击这些文件类型并在外部工具下:
- For .ui select Qt Creator/Designerand start UI designing
- For .qml select Qt Creatorand design UI in QML editor
- For .qrc select Qt Creatorand use resource editor
- For .cpp/.ui select LUpdateto create its translation file
- For .ts select Linguistand start the translating
- 对于 .ui 选择Qt Creator/ Designer并开始 UI 设计
- 对于 .qml 选择Qt Creator并在 QML 编辑器中设计 UI
- 对于 .qrc 选择Qt Creator并使用资源编辑器
- 对于 .cpp/.ui 选择LUpdate以创建其翻译文件
- 对于 .ts 选择Linguist并开始翻译
Automatic Beautifier
自动美化器
If in Qt Creatoryou used a beautifier like "Uncrustify" to auto beautify the code on saving a source file, then:
如果在Qt Creator 中您使用了“Uncrustify”之类的美化器来自动美化保存源文件时的代码,则:
- Install the plugin "Save Actions"
- Under the "File -> Settings -> Save Actions"
- Check:
- Active save actions on save
- Reformat file
- For best performance un-check:
- Organize imports
- 安装插件“保存操作”
- 在“文件 -> 设置 -> 保存操作”下
- 查看:
- 保存时的主动保存操作
- 重新格式化文件
- 为了获得最佳性能,请取消选中:
- 组织进口
回答by jtbr
As Tom Lank mentions, Qt projects can now be managed with, and built under CMake, which makes CLion happy.
正如 Tom Lank 所提到的,Qt 项目现在可以使用 CMake 进行管理和构建,这让 CLion 感到高兴。
Qt5's CMake manualdescribes how. Qt provides a lot of magic under the hood here, and it's explained much better in the CMake documentation.
Qt5 的 CMake 手册描述了如何。Qt 在幕后提供了很多魔法,在CMake 文档中对其进行了更好的解释。
One thing that isn't mentioned in the Qt CMake manual or above is that you'll also need the lines:
Qt CMake 手册或以上未提及的一件事是您还需要以下几行:
set(CMAKE_AUTOUIC ON) # if you have any .ui files
set(CMAKE_AUTORCC ON) # if you have any .qrc files
All of these calls to set()
should probably come before the line find_package(Qt5Widgets REQUIRED)
. Also include any .ui or .qrc files as dependencies in the call to add_executable()
along with your .cpp files.
所有这些调用都set()
应该在行之前出现find_package(Qt5Widgets REQUIRED)
。还包括任何 .ui 或 .qrc 文件作为调用中的依赖项add_executable()
以及 .cpp 文件。
This was initially very confusing to me from browsing the web, but you shouldn't need any calls to qt_*()
or qt5_*()
. These have been superseded so far as I can tell.
这最初让我在浏览网页时感到非常困惑,但您不应该需要对qt_*()
或 进行任何调用qt5_*()
。据我所知,这些已被取代。
To test that your CMakeLists.txt
actually works correctly, you can try building within Qt Creator, by loading CMakeLists.txt
as a project and building.
要测试您的CMakeLists.txt
实际工作是否正常,您可以尝试在 Qt Creator 中进行构建,方法是将其CMakeLists.txt
作为项目加载并构建。
Once confirmed, you can load the CMakeLists.txt
file as a project in CLion.
Most likely, you'll need to tell CMake where to find your Qt packages with a line like this before your find_package
's:
确认后,您可以将该CMakeLists.txt
文件作为项目加载到CLion 中。最有可能的是,您需要在您的find_package
's之前用这样的一行告诉 CMake 在哪里可以找到您的 Qt 包:
set(CMAKE_PREFIX_PATH "C:/Qt/5.9/msvc2015_64")
Finally, if you're running on / building for windows, Qt no longer comes pre-built with GCC/Mingw32 libraries. You need to build with visual studio. Luckily, CLion now supports Visual Studio experimentallyand I've found it to work for Qt projects; just be sure to set the architecture (under Settings->Build, Execution, Development->CMake) to x86_amd64, in order to build in 64-bit mode and be compatible with Qt's pre-build libs.
最后,如果您在 Windows 上运行 / 构建,Qt 不再带有 GCC/Mingw32 库的预构建。您需要使用 Visual Studio 进行构建。幸运的是,CLion 现在实验性地支持 Visual Studio,我发现它适用于 Qt 项目;只需确保将架构(在 Settings->Build, Execution, Development->CMake 下)设置为 x86_amd64,以便在 64 位模式下构建并与 Qt 的预构建库兼容。
All of this is tested with CLion 2017.1, Qt 5.9, and the Visual Studio 2015 compiler.
所有这些都使用 CLion 2017.1、Qt 5.9 和 Visual Studio 2015 编译器进行了测试。
回答by Halcyon
This link has a quickstart project, you just have to change your CMAKE_PREFIX_PATH in CMakeLists to the location of the Qt packaged compiler you want to use (mine is gcc_64, his by default is clang_64)-- it has some of the settings mentioned by other answers already set:
此链接有一个快速启动项目,您只需将 CMakeLists 中的 CMAKE_PREFIX_PATH 更改为您要使用的 Qt 打包编译器的位置(我的是 gcc_64,他的默认是 clang_64)——它有一些其他人提到的设置答案已经设定:
https://github.com/ArneGockeln/qtstarter
https://github.com/ArneGockeln/qtstarter
In addition, (on Ubuntu-based Linux) I had to install the OpenGL libraries as described here (https://askubuntu.com/questions/11378/how-do-i-set-up-an-opengl-programming-environment).
此外,(在基于 Ubuntu 的 Linux 上)我必须按照此处所述安装 OpenGL 库(https://askubuntu.com/questions/11378/how-do-i-set-up-an-opengl-programming-environment)。
回答by Tom
You can build QT applications in CLion. QT Provides CMake modules that take care of all details.
您可以在 CLion 中构建 QT 应用程序。QT 提供处理所有细节的 CMake 模块。
The following CMake script builds the example application 'Dynamic Layouts Example'
以下 CMake 脚本构建示例应用程序“动态布局示例”
cmake_minimum_required(VERSION 3.7)
project(qtlayoutexample)
set(CMAKE_CXX_STANDARD 14)
# Find QT packages
find_package(Qt5Widgets)
# Add the include directories for the Qt 5 Widgets module to
# the compile lines.
include_directories(${Qt5Widgets_INCLUDE_DIRS})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Add compiler flags for building executables (-fPIE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
qt5_generate_moc(main.cpp main.moc)
# Tell CMake to create the qtlayoutexample executable
add_executable(qtlayoutexample main.cpp dialog.cpp main.moc)
#Link the qtlayoutexample executable to the Qt 5 widgets library.
target_link_libraries(qtlayoutexample Qt5::Widgets)
More information regarding building Qt applications with CMake.
回答by Roland Wolf
You can easily develop Qt with VC, Eclipse, CLion etc. when you use CMake as a build tool. CMake will generate the project files for each IDE. I was using several IDEs this way. After this journey I am an even happier user of Qt Creator.
当您使用 CMake 作为构建工具时,您可以使用 VC、Eclipse、CLion 等轻松开发 Qt。CMake 将为每个 IDE 生成项目文件。我以这种方式使用了几个 IDE。在这次旅程之后,我对 Qt Creator 的用户更加满意了。
回答by brgs
The only thing you need is to add QT install ..Qt\5.10.1\mingw53_32\bin;
to your PATH. Don't forget to restart PC afterwards, because CLion for some reason isn't able to refresh the path, only full pc restart helps.
您唯一需要做的就是将 QT 安装添加..Qt\5.10.1\mingw53_32\bin;
到您的 PATH。之后不要忘记重启电脑,因为 CLion 由于某种原因无法刷新路径,只有完全重启电脑才有帮助。