How to configure CLion IDE for Qt Framework? - c++

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.

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.

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:
Create a C++ Executable/Library project
Use this sample "CMakeLists.txt" for common console/gui projects that uses: QtCore, QtWidgets and QtQuick
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:
For .ui select Qt Creator/Designer and start UI designing
For .qml select Qt Creator and design UI in QML editor
For .qrc select Qt Creator and use resource editor
For .cpp/.ui select LUpdate to create its translation file
For .ts select Linguist and start the translating
Automatic Beautifier
If in Qt Creator you used a beautifier like "Uncrustify" to auto beautify the code on saving a source file, then:
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

As Tom Lank mentions, Qt projects can now be managed with, and built under CMake, which makes CLion happy.
Qt5's CMake manual describes how. Qt provides a lot of magic under the hood here, and it's explained much better in the CMake documentation.
One thing that isn't mentioned in the Qt CMake manual or above is that you'll also need the lines:
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.
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.
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.
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:
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 experimentally and 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.
All of this is tested with CLion 2017.1, Qt 5.9, and the Visual Studio 2015 compiler.

You can build QT applications in CLion.
QT Provides CMake modules that take care of all details.
The following CMake script builds the example application 'Dynamic Layouts Example'
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.

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:
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).

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.

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.

Related

AUTOMOC set to true makes fail cmake build

I'm at the very first day of Qt + Cmake and Conan, trying to make things work. I'm not using qmake because I'll integrate everything into a bigger project using cmake.
By following QT's tutorial, I figured out that I need to compile QT macros, and for that there's a useful AUTOMOC CMake property, as suggested here.
The point is that it's making me fail cmake builds.
My conanfile.txt:
[requires]
qt/5.15.2
[generators]
cmake
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(qttest)
set(CMAKE_CXX_STANDARD 20)
set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)
set (PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
with the following output:
CMake Warning (dev) in CMakeLists.txt:
AUTOGEN: No valid Qt version found for target qttest. AUTOMOC disabled.
Consider adding:
find_package(Qt<QTVERSION> COMPONENTS Core)
to your CMakeLists.txt file.
This warning is for project developers. Use -Wno-dev to suppress it.
ouch, but adding the find doesn't make things better:
CMake Warning at CMakeLists.txt:6 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5", but
CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Actually the project compiles, Qt is there in its conan dir:
matteo#MacBook-Pro-de-matteo 96a68a791abfc7a246f2bc28aa2f6fc210be0f9f % cd ~/.conan/data/qt
matteo#MacBook-Pro-de-matteo qt % ls
5.15.2 6.2.2
matteo#MacBook-Pro-de-matteo qt %
how could I enable it, or make things easier to compile it along with cmake?
You need to tell CMake, where to find Qt.
So, as CMake suggests by itself:
find_package(Qt5 COMPONENTS Core)
for the most basic stuff, you might want to add some of the other components later.
Depending on the system you are working on and your Qt installation, you need to tell CMake where to search for the package configuration files (second error message). CMake has some default directories, where it looks for these files, but obviously, there is none. On Linux, this can be solved by installing Qt with a package manager (this will install the CMake config files to one of the Qt default locations). If you are on Windows or if you installed Qt to a different location, this can be solved by providing the path with the PREFIX_PATH-variable.
cmake -B $BUILD_DIR -S $SOURCE_DIR -DCMAKE_PREFIX_PATH=$QT_INSTALL_PATH/5.15.2/$ARCHITECTURE $OTHER_OPTIONS
(You can have different versions installed in the same installation path, that's why Qt adds an other folder with the version number. And you can have different compilers/architectures. On Windows for example, you might have a mingw73_32 and a msvc2017 folder to choose.)
As already mentioned in the comments, a project is no CMake target. CMake targets are either libraries (add_library), executables (add_executable) or custom targets (add_custom_target); the project is not. If you want to set the AUTOMOC property target wise, that's ok and even suggested by CMake, but you can also set it globally by using:
set(CMAKE_AUTOMOC ON)

Using Qt5 installed via VCPKG in Visual Studio for C++

I know this is a daft question, but I'm a beginner in visual studio/c++/cmake. I'm looking for a quick intro on how to use Qt5 installed via vcpk using:
vcpkg install qt5-base:x64-windows
This all installed ok and I got the following:
The package qt5-base:x64-windows provides CMake targets:
find_package(Qt5Concurrent CONFIG REQUIRED)
target_link_libraries(main PRIVATE Qt5::Concurrent Qt5::ConcurrentPrivate)
etc....
I just don't know what to do next! Before using libs in VS I just did an <#include> now I'm confronted with this lot... Pref. I want some sort of explanation at newbie level please.
If I add the line (at the top of a .cpp file just as a test):
#include <QtWidgets/QApplication>
It gives: Error (active) E1696 cannot open source file "QtWidgets/QApplication"
I'm new, I thought vcpkg took all the pain out of having to add all the libs etc to the project options? What do I need to do?
If you ran vcpkg integrate install and are just using VS you can just #include <Qt5/QtWidgets/QApplication>
If you are using CMake:
find_package(Qt5 COMPONENTS Widgets Concurrent CONFIG REQUIRED) and use target_link_libraries as described by the other answers. But you probably have to switch to #include <QApplication> since cmake file add the QtWidgets folder to the include folders.
For find_package to find the vcpkg build versions you have to specify the vcpkg.cmake toolchain file as the CMAKE_TOOLCHAIN_FILE=<vcpkgroot>/scripts/buildsystems/vcpkg.cmake (must be set in the first CMake call or rather early in the CMakeLists.txt before any project() call) and maybe also VCPKG_TARGET_TRIPLET=<sometriplet> (must also be defined early before CMAKE_TOOLCHAIN_FILE is loaded) if you installed Qt5 using one of the static triplets.
vcpkg is a cross-platform C++ package manager. Unlike winget, apt, and brew, vcpkg is designed for developers. For example, it builds the binaries from the source by default.
So it help user to have libraries installed in their projects and be able to find them.
You still need to learn how CMake canonical find_package() work IMHO.
Qt provide a cmake config package and usually, you'll need to use
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
then
target_link_libraries(main PRIVATE ... Qt5::Core Qt5::Gui Qt5::Widgets)
ie rule of Thumb: you need a QtWidget/* include ? then target_link to Qt5::Widget etc...
Please note that CMake also provides (i.e. built-in) a few tools to ease Qt-related dev...
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
-> you should try to read the CMake doc...

How to create GTK3 app with CMake and Eclipse CDT on Windows

I trying create simple gtk3 app for windows. Gtk3 has been installed with official instruction by mysys. I created cmake app in eclipse. I tried create simple gtk3 app and it compiled successful, but Eclipse show errors. And when I run compiled app it failed.
CMakeList.txt
cmake_minimum_required (VERSION 2.6)
project (EITSoftGTK)
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
add_executable(EITSoftGTK EITSoftGTK.cpp)
# Link the target to the GTK+ libraries
TARGET_LINK_LIBRARIES(EITSoftGTK ${GTK3_LIBRARIES})
As I remarked in my comment, you need to make sure all dependencies are accessible. The official documentation describes how DLLs are located, but by far the easiest is to copy the missing DLLs (libgio... and libgobject...) into the same directory as your executable.

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl

Setup CMake with SFML in VS2017

Just like in CLion I want to use SFML with Visual Studio 2017, but I'm still learning cmake and I don't know the commands or the logic of how cmake works at all. I've just seen some posts and got this litle script.
Note: I downloaded the latest version of sfml in the link provided, I just taked the extrated directory and put alongside CMakeLists.txt in my folder
#sets up the minimum version of cmake
cmake_minimum_required(VERSION 3.9)
#how the project will be called
project (space_impact)
#set c++11 standard
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" -std=c++11)
#set source files
set (SOURCE_FILES main.cpp)
#we add the executable of the program
add_executable (space_impact ${SOURCE_FILES})
#taked from a mac-clion tutorial, doesn't work
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFML/cmake-modules/")
find_package (SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(space_impact ${SFML_LIBRARIES})
endif()
that thing gave me errors:
Error CMake Error at SFML/cmake-modules/FindSFML.cmake:355 (message):
Could NOT find SFML (missing: SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY
SFML_GRAPHICS_LIBRARY SFML_NETWORK_LIBRARY SFML_AUDIO_LIBRARY) SFML/cmake-modules/FindSFML.cmake
I want everything to be dynamic, but I don't know how can I do that..
So my question is what should I do for setting up correctly SFML with Cmake in Visual Studio.
I don't want the old-fashioned method from the official website
UPDATE
Here's my location....
The thing is.. the FindSFML.cmake script it's not working...
What files should I move for make it working?
Your script is perfectly fine, except three things I'd change:
Move the whole module detection before defining targets. I'm pretty sure you also have to define your include directories before.
Your if(SFML_FOUND) bracket is pretty pointless right now, because you've set SFML to be required, which means it will never get past find_package() unless it's found.
-std=c++11 is a GCC only flag (MSVC will always use the latest standard, unless specified). As such you'll have to check the compiler here or use CMAKE_CXX_STANDARD.
So the "cleaned" CMakeLists.txt could look like this:
#sets up the minimum version of cmake
cmake_minimum_required(VERSION 3.9) # personally I'd set this version as low as required; you don't have to require the cutting edge version
#how the project will be called
project (space_impact)
#set the C++ standard to be used
set (CMAKE_CXX_STANDARD 11)
#set source files
set (SOURCE_FILES main.cpp)
#look for SFML and add it
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFML/cmake-modules/")
find_package (SFML REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
#we add the executable of the program
add_executable (space_impact ${SOURCE_FILES})
target_link_libraries(space_impact ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
Note that adding SFML_DEPENDENCIES to the library list is optional, unless you're using a static version of SFML.
But what about your SFML issue? Since you don't have the SFML files installed in any directory looked into by default, you'll have to tell CMake where it's found using the CMake variable SFML_ROOT or the environment variable of the same name.
So the first time you're invoking CMake, it could look like this:
cmake -G "Visual Studio 15 2017" -DSFML_ROOT=path/to/sfml path/to/source
This is all you need to compile sfml in your cmake project.
find_package(SFML 2.5.1 COMPONENTS system graphics audio network REQUIRED)
add_executable (AwesomeProject "AwesomeProject.cpp" "AwesomeProject.h")
target_link_libraries(AwesomeProject PRIVATE sfml-audio sfml-graphics sfml-network sfml-system)
Also set SFML_DIR var to your sfml folder.