Although CLion is not a good choice for writing Qt programs, it can still be used by those who are accustomed to JetBrains IDEs.
Environment used by the author:
- Windows 11 22H2
- Qt 5.15.2
- CLion 2022.3.1
Preparations#
1. Download CLion#
No need for further explanation.
University students can apply for free use, see JetBrains Student Products for details.
2. Download Qt#
- Since Qt no longer provides offline installers for individual users, you can only download the online installer here, and choose the version that corresponds to your system.
- Register a Qt account, agree to the open source obligation, wait, and choose the installation directory.
- When selecting components: (you can refer to the images)
- First filter on the right side, select only LTS.
- For Qt Design Studio, I chose version 3.9.0, although it seems unnecessary, it's still good to have one selected.
- Under the Qt directory, select Qt 5.15.2. I chose both the 32-bit and 64-bit versions of MingGW, but MSVC is also an option. Choose the rest according to your needs.
- Under Developer and Designer Tools, select QtCreator and a few others, CMake, and Ninja must be selected. The main functions of the options are as follows (source unknown, accuracy unknown):
- CDB Debugger (command line debugger): Console debugger, the native debugger for MSVC in Qt. If you choose it, you need to check it. If you choose MinGW, it is not necessary because MinGW has the GDB debugger.
- MingGW: The MingGW here is used for cross-compilation, generating executable code on one platform for another platform.
- Strawberry Perl: Perl language tools.
- Follow the instructions to proceed.
- It's okay if you missed some components, you can find the MaintenanceTool.exe in the installation directory to make changes.
Configuration#
You can refer to JetBrains' documentation Qt projects | CLion Documentation (jetbrains.com) for more information.
- Open CLion, create a new project -> Qt Widgets Executable.
- Fill in the location and C++ standard by yourself, choose Qt version 5.
- Qt CMake prefix path: Fill in "%qt installation path%/version number/compiler folder".
- For example, mine is "C:/Environments/qt/5.15.2/mingw81_64"; the example given by JetBrains is "C:\Qt\Qt5.14.0\5.14.0\mingw73_32\".
- No need for double quotes, using / or \ should both work fine. If not, try using the other one.
- After clicking OK, CLion will automatically generate a sample code and a CMakeList.txt. You can run it directly now. If it doesn't work, you can reload the CMake project.
- If it still doesn't work, check if you did the third step correctly. If you set the prefix path incorrectly at the beginning, as far as I know, you need to make changes in at least two places:
- In the CMakeList.txt file, modify
set(CMAKE_PREFIX_PATH "%your prefix path%")
. - In the Build and Run configurations.
So if you made a mistake, I think the best way is to start over and create the project again.
- In the CMakeList.txt file, modify
External Tools#
- Through QtCreator, you can easily edit .ui and .qrc files. Our goal is to use it to edit Qt-specific file types.
- Open File -> Settings -> Tools -> External Tools, and create a new entry.
Change the link of "Program" to the location of your own QtCreator. - In the future, when you want to edit .ui or .qrc files, you can right-click on them, go to External Tools -> QtCreator.
Others#
- If you still have problems, you can refer to the JetBrains documentation mentioned above. It provides a more thorough description of using CLion to write Qt programs.
- I won't mention the advantages of using CLion to write cpp programs. For Qt, the biggest headache for CLion is that its built-in debugging tool cannot display the contents of Qt data types, such as QString and QList. For QString, qDebug() cannot output its value during debugging. A clumsy method is to use the toStdString method of QString to view the value through the debugging tool.