QtCreator is always the best choice when developing programs using Qt. As a veteran IDE, it provides powerful features and also has many optimizations for Qt support. If there is no special reason you should use it.

However, a handy tool will greatly increase productivity, and if you have used other tools for development, then consider the cost of learning to use QtCreator.

So I will introduce the configuration of CLion (another powerful c++ IDE) as a development environment for Qt5, while avoiding the installation of another large software while leveraging the existing toolchain.

Ready to work

The installation and activation of CLion is beyond the scope of this article, and we assume that you have already installed CLion. If you need help, go to the CLion website to find out how to install it.

CLion uses GCC for project construction by default. However, with GCC, code completion is not available, so we replace toolchains with clang:

CLion QT5 toolchains

CLion QT5 toolchains

Note that CLion comes with a cmake. If we want to use some of the cmake modules provided by the system, we must replace it with the system’s cmake as shown.

Then make sure your compiler supports at least C++11. If you can support C++14 or C++17, it is naturally best.

Create a Qt5 project

The following is the creation of the Qt5 project, CLion does not distinguish between ordinary C++ projects and Qt projects (after all, Qt projects are generally c++ projects or C++/QML projects), so you can create them like this:

CLion QT5 Create project

CLion QT5 Create project

After the creation is completed, the project structure is as shown:

CLion QT5 project structure

CLion QT5 project structure

cmake-build-releaseThe directory is the intermediate code for generating tools such as moc and the executable file. Of course, the executable path can be specified by itself. This is the default option of CLion.

At this point compile run and code completion is still not available, so next we set the compilation and code completion.

Set Qt code completion and project compilation

One point to note before is that CLion CMakeLists.txtorganizes and sets up projects based on files, so if we need to add a third-party library such as Qt or change the compilation behavior, we can just modify it CMakeLists.txt.

So we need to CMakeLists.txtmodify it to look like this:

cmake_minimum_required(VERSION 3.0)

# c++ version

# auto call moc,uic,rcc handle qt extension


# Qt5 cmake directory
# QT_DIR and QT_VERSION is qt install directory and varible
# use system cmake,or your cmake or cmake with CLion

# qt moduble,name is qmake中QT += <name> name with Qt5
find_package(Qt5Widgets REQUIRED)

aux_source_directory(. DIRS_SRCS)

add_executable(test ${DIRS_SRCS})

# link Qt module
target_link_libraries(test Qt5::Widgets)

It’s worth noting that one of the benefits of using the system-provided Qt is that the compiled program can use the system’s styles and themes, but the downside is that the version may be inconsistent with the one we want to use and generate an error.

After modifying, click on the picture Reload changes:

CLion QT5 cmake

CLion will automatically generate a new Makefile for compiling the project, and now code completion is also available.

Compile project

Let’s write a small example so that a spinbox and slider can be linked to each other:

#include <QApplication>
#include <QFormLayout>
#include <QtGlobal>
#include <QObject>
#include <QSlider>
#include <QSpinBox>
#include <QWidget>

int main(int argc, char *argv[])
    QApplication app(argc, argv);

    auto window = new QWidget;

    auto spinBox = new QSpinBox;
    spinBox->setRange(0, 100);
    auto slider = new QSlider;
    slider->setRange(0, 100);

    auto layout = new QFormLayout;
    layout->addRow("spin box:", spinBox);
    layout->addRow("slider:", slider);

    return app.exec();

Then we click int main(...)on the run button on the left:

The program starts compiling:

CLion QT5 Compile

CLion QT5 Compile

If the program is compiled successfully, it will run automatically:

CLion QT5 demo

At this point, CLion can already cooperate with Qt for development.