How to configure CLion IDE for Qt Framework? Is this IDE compatible with Qt, or are there other IDEs compatible with Qt?
I just want to try to use something else than Qt Creator.
In CLion, go to Settings( Ctrl+Alt+S ), navigate to Build, Execution, Deployment | Toolchain and select the toolchain that matches your Qt installation.
In CLion, go to File | Settings | Build, Execution, Deployment | Toolchains and select the Visual Studio toolchain that you want to configure, or create a new one. Point the C Compiler and C++ Compiler fields to clang-cl.exe. CLion will suggest the paths detected automatically.
In CLion, you can use GCC-based compilers, Clang, Clang-cl, Visual Studio C++ compiler, as well as IAR compiler and custom-defined compiler.
Introduction. CMake can find and use Qt 4 and Qt 5 libraries. The Qt 4 libraries are found by the FindQt4 find-module shipped with CMake, whereas the Qt 5 libraries are found using "Config-file Packages" shipped with Qt 5.
UPDATE
There is an now an official tutorial on the Jet Brain website:
https://www.jetbrains.com/help/clion/qt-tutorial.html
Original answer
This approach is one of the simplest way to be used for the newest Qt version.
Qt: 5.10.1 CLion: 2018.1.2 GDB: 8.1
Project setup
In 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)
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 QQmlApplicationEngine at runtime
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:
Pretty printers for Qt5:
1- Lekensteyn Qt5 Pretty Printers (Working):
Address: https://github.com/Lekensteyn/qt5printers
Setup: ~/.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!!!):
"PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/share/qtcreator/debugger/"
Setup: ~/.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:
Qt Creator:
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:
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:
Automatic Beautifier
If in Qt Creator you used a beautifier like "Uncrustify" to auto beautify the code on saving a source file, then:
I was as desperate as you, until I read this Quora discussion. It worked perfectly for me!
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'):
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:
-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
:
#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:
find_package(Qt5Sql REQUIRED)
just after the other find_package
, and adding in the last line:
target_link_libraries(myqtproject Qt5::Widgets Qt5::Sql)
You can do this with all the other Qt5 modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With