如何为Qt框架配置CLion IDE?这个IDE与Qt兼容吗,还是有其他与Qt兼容的IDE?
我只是想尝试使用除Qt Creator之外的其他工具。
如何为Qt框架配置CLion IDE?这个IDE与Qt兼容吗,还是有其他与Qt兼容的IDE?
我只是想尝试使用除Qt Creator之外的其他工具。
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
cmake
二进制文件。为此,请转到:
“首选项”->“构建、执行、部署”->“CMake”,并在“CMake选项”中添加Qt使用的CMake路径,该路径应位于安装Qt的目录中。例如,在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
更新
我卡在了添加Qt5模块的问题上(例如QSql)。您可以通过在CMakeLists.txt中添加以下内容来解决此问题:
find_package(Qt5Sql REQUIRED)
find_package
之后,在最后一行添加:target_link_libraries(myqtproject Qt5::Widgets Qt5::Sql)
更新
现在Jet Brain官网上有一个官方教程:
https://www.jetbrains.com/help/clion/qt-tutorial.html
原回答
这种方法是用于最新的Qt版本中最简单的方法之一。
Qt: 5.10.1
CLion: 2018.1.2
GDB: 8.1
项目设置
在 CLion 中:
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)
要使moc能够对资源文件和内部文件(如qml文件和文本文件)进行处理并使其可访问,应将资源文件(.qrc)添加到add_executable列表中。
在运行时,应该将qml文件包含在一个qrc文件中,并使用QQmlApplicationEngine进行加载。
调试器:
为了在Qt类型的调试会话中具有可读性,必须在系统上安装新的GDB并提供漂亮的打印机:
适用于Qt5的漂亮打印机:
1- Lekensteyn Qt5 Pretty Printers (可用):
地址: https://github.com/Lekensteyn/qt5printers
设置: ~/.gdbinit
python
import sys, os.path
sys.path.insert(0, os.path.expanduser('~/.gdb'))
import qt5printers
qt5printers.register_printers(gdb.current_objfile())
end
2- 官方(不可用!):
"PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/share/qtcreator/debugger/"
设置:~/.gdbinit
如包含的 readme 文件所述(但不起作用!):
python sys.path.insert(1, '<path/to/qtcreator>/share/qtcreator/debugger/')
python from gdbbridge import *
外部工具
在“文件 -> 设置 -> 工具 -> 外部工具”中,添加4个外部工具:
Qt Creator:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/bin/qtcreator"
Arguments: $FilePath$
用户界面设计师:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/designer")
Arguments: $FilePath$
更新:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/lupdate")
Arguments: $FilePath$ -ts $FileNameWithoutExtension$.ts
语言学家:
Program: "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/linguist")
Arguments: $FilePath$
现在你可以右键点击这些文件类型并选择外部工具:
代码自动格式化
如果在 Qt Creator 中你使用了像“Uncrustify”这样的代码美化插件来在保存源文件时自动美化代码,那么:
set(CMAKE_AUTOUIC ON) # if you have any .ui files
set(CMAKE_AUTORCC ON) # if you have any .qrc files
set()
的调用都应该放在find_package(Qt5Widgets REQUIRED)
这行代码之前。此外,将任何.ui或.qrc文件与您的.cpp文件一起作为add_executable()
的依赖项包含进去。qt_*()
或qt5_*()
的调用,因为它们已经过时了。CMakeLists.txt
是否正确工作,您可以尝试在Qt Creator中构建,通过将CMakeLists.txt
作为项目加载并进行构建。CMakeLists.txt
文件作为项目加载。很可能,您需要在find_package
之前使用类似于以下内容的代码告诉CMake在哪里找到您的Qt软件包:set(CMAKE_PREFIX_PATH "C:/Qt/5.9/msvc2015_64")
最后,如果您正在运行/构建Windows,Qt不再预先构建GCC/Mingw32库。 您需要使用Visual Studio进行构建。 幸运的是,CLion现在试验性地支持Visual Studio,我发现它适用于Qt项目; 只需确保将体系结构(在“设置” ->“构建,执行,开发” ->“CMake”下)设置为x86_amd64,以在64位模式下构建并与Qt的预构建库兼容。
所有这些都经过了CLion 2017.1,Qt 5.9和Visual Studio 2015编译器的测试。
https://github.com/ArneGockeln/qtstarter
此外,(在基于Ubuntu的Linux上)我还需要按照这里描述的方式安装OpenGL库(https://askubuntu.com/questions/11378/how-do-i-set-up-an-opengl-programming-environment)。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)
..Qt\5.10.1\mingw53_32\bin;
添加到你的PATH环境变量中。别忘了在此之后重启电脑,因为CLion无法刷新路径,只有完全重启电脑才能帮助解决问题。