P
P
Profi_GMan2018-01-01 16:42:40
Qt
Profi_GMan, 2018-01-01 16:42:40

How to create a qt gui project with cmake?

Good day!
There is a graphical application in c++ qt. He originally used qmake.
Then I decided that I needed to switch to cmake. Wrote this cmakelists.txt:

cmakelists.txt
cmake_minimum_required (VERSION 2.6)
project(yourdroid)
option(QT5BUILD "Build using Qt5 libs" TRUE)
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Widgets finds its own dependencies (QtGui and QtCore).
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core)
find_package(Qt5 COMPONENTS Widgets Core REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
# The Qt5Widgets_INCLUDES also includes the include directories for
# dependencies QtCore and QtGui
include_directories(${Qt5Core_INCLUDES})
# We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
add_definitions(${Qt5Widgets_DEFINITIONS})
# Executables fail to build with Qt 5 in the default configuration
# without -fPIE. We add that here.
set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
#qt5_use_modules(yourdroid core gui widgets)
set(yourdroid_SOURCES "data.cpp" "log.cpp" "main.cpp" "window.cpp" "install.cpp" "cmd.cpp" "console.cpp" "bkisofs/bkAdd.c" "bkisofs/bkCache.c" "bkisofs/bkDelete.c" "bkisofs/bkError.c" "bkisofs/bkExtract.c" "bkisofs/bkGet.c" "bkisofs/bkIoWrappers.c" "bkisofs/bkLink.c" "bkisofs/bkMangle.c" "bkisofs/bkMisc.c" "bkisofs/bkPath.c" "bkisofs/bkRead.c" "bkisofs/bkRead7x.c" "bkisofs/bkSet.c" "bkisofs/bkSort.c" "bkisofs/bkTime.c" "bkisofs/bkWrite.c" "bkisofs/bkWrite7x.c")
set(yourdroid_HEADERS "data.h" "log.h" "window.h" "enum.h" "install.h" "version.h" "cmd.h" "console.h" "bkisofs/bk.h" "bkisofs/bkAdd.h" "bkisofs/bkCache.h" "bkisofs/bkDelete.h" "bkisofs/bkError.h" "bkisofs/bkExtract.h" "bkisofs/bkGet.h" "bkisofs/bkInternal.h" "bkisofs/bkIoWrappers.h" "bkisofs/bkLink.h" "bkisofs/bkMangle.h" "bkisofs/bkMisc.h" "bkisofs/bkPath.h" "bkisofs/bkRead.h" "bkisofs/bkRead7x.h" "bkisofs/bkSet.h" "bkisofs/bkSort.h" "bkisofs/bkTime.h" "bkisofs/bkWrite.h" "bkisofs/bkWrite7x.h")
add_executable(yourdroid ${yourdroid_SOURCES} ${yourdroid_HEADERS} ${Qt})

But when compiling, the error "QObject not found" is produced. That is, cmake did not find \\ did not connect QtCore.
Then I found that qt5_use_modules can be used. I inserted qt5_use_modules(yourdroid core gui widgets), but cmake says that the existing target should be the first parameter. Where can I get/create it?
zy In cmake I'm a beginner
zyy I also heard about qt5_wrap_ui, but I didn't even try it because you must first solve that problem
Many thanks in advance! Already smoked all the mana, shoveled the whole Yandex ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Profi_GMan, 2018-01-01
@Profi_GMan

Didn't notice an important detail, qt5_use_modules should be after add_executable, everything worked

V
Vitaly, 2018-01-01
@vt4a2h

I already answered something like this: How to connect qt to cmake?
Start with a minimal project of a couple of files. It will start, add everything else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question