4

I recently started to refresh my C++ knowledge (haven't used it for like 10 years~). I chose to use QtCreator, since I wanted to learn more about Qt which I only tried once.

Now, I created a qmake project and it all works just fine, but I would also like to learn about cmake and I thought of converting the project to cmake for that purpose.

Unfortunately my knowledge related to building process of C++ is very small, I usually just let IDE to handle all that stuff for me, since my projects were rather small.

Now, I would like to ask you to help me move my qmake .pro file to cmake CMakeLists.txt, it's fairly simple.

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = model_view_playground
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

QMAKE_CXXFLAGS += -std=c++0x

SOURCES += main.cpp\
        mainwindow.cpp \
    testitemlistmodel.cpp

HEADERS  += mainwindow.h \
    testclass.h \
    testitemlistmodel.h

FORMS    += mainwindow.ui

DISTFILES +=

I do not like to get "plug and play" solutions usually, but before I dive into cmake, I'd like to have this converted and be able to build it.

As for building, it's kind of a different question, but perhaps someone can answer this one as well - do I need to make any changes in QtCreator to make it use cmake instead of qmake in my project (or ideally keep them side by side to be able to switch between cmake and qmake)?

I already have CMake added in Build & Run -> CMake (it was autodetected).

1
  • I do not like to get "plug and play" solutions usually, but ... - We don't like such questions too. Have you ever tried to search Stack Overflow before asking? There are already [8 questions]([cmake] [qmake]convert) about QT->CMake conversion.
    – Tsyvarev
    Jun 23, 2017 at 19:57

3 Answers 3

10

The difference between qmake and cmake for Qt programing is that qmake already knows where to find the Qt Libraries and Sources. CMake doesn't know that and you probably have point to right location of the library. Usually you will declare a environment variable QT_DIR. I will put an example using Qt5 since I forgot how to create a Qt4 project in cmake

project(Qt5Project) # Your project name

set(CMAKE_CXX_STANDARD 11) # This is equal to QMAKE_CXX_FLAGS += -std=c++0x

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Instruct CMake to run uic automatically when needed.
set(CMAKE_AUTOUIC ON)

# This will find the Qt5 files. You will need a QT5_DIR env variable
find_package(Qt5Widgets REQUIRED) # Equivalent of QT += widgets

set(SOURCES main.cpp mainwindow.cpp testitemlistmodel.cpp) 
set(HEADERS mainwindow.h testclass.h testitemlistmodel.h)
set(UI mainwindow.ui)

# This will create you executable
add_executable(model_view_playground ${SOURCES} ${HEADERS} ${UI})
# This will link necessary Qt5 libraries to your project
target_link_libraries(model_view_playground Qt5::Widgets)

For a simple project a script like this will serve.

QtCreator already has support for cmake projects, you only need to open a folder with a CMakeLists.txt inside it and the IDE will recognize as a cmake project

3
  • lovely, thank you. It seems pretty easy, just a small question regarding the first line of .pro file QT += core gui. I don't see any kind of reference in cmake file, does that mean it can be omitted? And actually this is Qt5 (5.8) project anyway. So the QT5_DIR I understand should be configured per project? Or can I configure it for the "kit" (Options -> Build & Run -> Kits). I did check my project build environment and it seems my System environment has QTDIR already defined. Will that be enough?
    – pzaj
    Jun 23, 2017 at 11:59
  • When you include Qt5Widgets, Qt5Core and Qt5Gui are included since Qt5Widgets needs then. QT5_DIR must be configured in OS enviroment variable. The other option is point to right location in the script, but this is a bit more complicated Jun 23, 2017 at 17:57
  • thank you for the explanation. Well, I assume the QTDIR (it does indeed point to Qt5.8 for gcc) has been set during Qt framework installation then, so one thing less to do :) I will try it out tomorrow and mark your answer as correct if everything goes right. A bonus question if I may - is there any way to have the same project in QtCreator compile with qmake or cmake without closing the project (e.g. by using separate build and run configurations) or do I need to close my project and open project with either .pro or CMakeLists.txt to use qmake or cmake respectively?
    – pzaj
    Jun 23, 2017 at 22:25
2

Alternatively, for such a simple project (one executable, no libs) you could use my script cmake-project, which is an equivalent of qmake -project : https://github.com/KDAB/KDToolBox/tree/master/qt/cmake-project

Just run it in your source dir and it will generate a CMakeLists.txt to build all sources as a single executable.

0

One may consider using qmake2cmake.

These are Python scripts provided by Qt that enables to convert a single pro file or a whole project tree.

qmake2cmake ~/projects/myapp/myapp.pro will just convert myapp.pro to a CMakeLists.txt file along myapp.pro.

qmake2cmake_all ~/projects/myapp will convert the whole project tree under ~/projects/myapp

Refer to qmake2cmake for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.