Migrating from Qt4 to Qt5 issues
-
I know there have been a number of posts regarding migrating code from Qt4 to Qt5, but none of the answers provided are working for me. I have a project that is built in KDevelop, which uses a cmake build system by default. The older project uses cmake and I'm not in a position to switch away from it.
My problem is that I am getting is that my include commands, such as
#include <QtWidgets>
come back with a 'QtWidgets' file not found error. My CMakeLists.txt file is
project(q5analyzer) cmake_minimum_required(VERSION 3.10) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../../../cmake/Modules/") find_package(Qt5 COMPONENTS Core Widgets Gui REQUIRED) # find_package(CFitsio REQUIRED) find_package(CCfits REQUIRED) # find_package(GSL REQUIRED) find_package(GOMP REQUIRED) find_package(HealPIX REQUIRED) set(qcanalyzer_SRCS main.cpp mainwindow.cpp controldatadlg.cpp mapperdlg.cpp mapselectdlg.cpp graphdlg.cpp graphselectdlg.cpp pixelizerdlg.cpp healpixdlg.cpp transformerdlg.cpp rshtdlg.cpp spectrumdlg.cpp) set(qcanalyzer_MOC_HDRS mainwindow.h controldatadlg.h mapperdlg.h mapselectdlg.h graphdlg.h graphselectdlg.h pixelizerdlg.h healpixdlg.h transformerdlg.h rshtdlg.h spectrumdlg.h) set(qcanalyzer_UIS mainwindow.ui controldatadlg.ui mapperdlg.ui mapselectdlg.ui graphdlg.ui graphselectdlg.ui pixelizerdlg.ui healpixdlg.ui transformerdlg.ui rshtdlg.ui spectrumdlg.ui) set(qcanalyzer_RCS mainwindow.qrc) qt_add_resources(qcanalyzer_RC_SRCS ${qcanalyzer_RCS}) qt_wrap_ui(qcanalyzer_UI_HDRS ${qcanalyzer_UIS}) qt_wrap_cpp(qcanalyzer_MOC_SRCS ${qcanalyzer_MOC_HDRS}) include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/..) link_directories(${CMAKE_CURRENT_BINARY_DIR}/../../libanalyzer/ ${CMAKE_CURRENT_BINARY_DIR}/../../libgraphics/) add_executable(q5analyzer ${qcanalyzer_SRCS} ${qcanalyzer_MOC_SRCS} ${qcanalyzer_UI_HDRS} ${qcanalyzer_RC_SRCS}) target_link_libraries(q5analyzer ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} graphics analyzer ${GOMP_LIBRARIES} ${HEALPIX_LIBRARIES} ${CCFITS_LIBRARIES} ${CFITSIO_LIBRARIES} ${GSL_LIBRARIES}) # install(TARGETS qcanalyzer RUNTIME DESTINATION bin)
What do I need to do so that the header files are found and I can build the program?
-
Hi,
You are not linking to the widgets module.
-
Why not simply add Qt5::QtCore to target_link_libraries and try it out. After that you can read about imported targets and what advantage they have - it's not just linking against the lib.
-
@Christian-Ehrlicher I modified the target_link_libraries to include QtCore. The line now reads
target_link_libraries(q5analyzer Qt5::QtCore graphics analyzer ${GOMP_LIBRARIES} ${HEALPIX_LIBRARIES} ${CCFITS_LIBRARIES} ${CFITSIO_LIBRARIES} ${GSL_LIBRARIES})
However, when kdevelop processes it, I get the following error
Target "q5analyzer" links to target "Qt5::QtCore" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?
I was able to confirm that find_package(Qt5 COMPONENTS Core Widgets Gui REQUIRED) is finding the package, so I'm at a loss as to why I'm having these problems still.
-
I wrote it wrong - it's
Qt5::Core
as written in the documentation. -
I wanted to thank you. This did indeed work. I don't understand why. In my mind, target_link_libraries is similar to the old libtool linker step. It would come at the end of the process. Why is this needed to even get the compiler to find the header files? I would have thought that the find_package is what handles that. Either way, though, I got the code to build and run, so I'm a very happy camper again!