简体   繁体   中英

CLion and Qt5: tuning qmake

I have a set-up as explained elsewhere, eg here . I have Qt5 installed system-wide, and have the requisite lines in my CMakeLists.txt . My IDE is Clion. Everything in a simple GUI goes fine until I add Q_OBJECT macro (I want this to connect signals to slots). Now, when I do this, I get the undefined reference to vtable -type error, that is also found in abundance on the net. My confusion arises from the fact that some recommend using Qt5 -bundled cmake for your project, which essentially means "just for GUI" I need to change the toolchain. But some actually say nothing about it. What all say is that

Qt runs qmake every time Q_OBJECT is added/deleted

Now, how to capture that in my CMakeLists.txt ? -- the relevant part thereof is given below. I've seen moc and qmake inside /usr/lib/qt5/bin ; so how to communicate this to CLion ?

# ----- GUI part -----
# Qt5 inclusion
# The meta object compiler is one of the core functionality of Qt, it reads a C++ header file and if it finds a
# Q_OBJECT macro, it will produces a C++ source file containing meta object code for the class.
# It's the mechanism that allow signal and slots to work.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# set(CMAKE_PREFIX_PATH $ENV{QT_DIR}/$ENV{QT_VERSION}/gcc_64/lib/cmake)
set(CMAKE_PREFIX_PATH /usr/lib/qt5/bin/)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# Enable user interface compiler (UIC)
# The user interface compiler is a program that read XML from the .ui file
# generated by Qt Interface Designer and generate C++ code from it.
set(CMAKE_AUTOUIC ON)

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

set(CMAKE_MODULE_PATH /usr/lib/qt5)
# @see: https://stackoverflow.com/questions/51994603/cmake-qt5-undefined-reference-to-qprinterqprinterqprinterprintermode
SET(QT5_MODULES Widgets PrintSupport)
find_package(Qt5 COMPONENTS ${QT5_MODULES} REQUIRED)

add_subdirectory(${PROJECT_SOURCE_DIR}/extern/qcustomplot)
add_executable(gui
        ${PROJECT_SOURCE_DIR}/gui/main.cpp
        ${PROJECT_SOURCE_DIR}/extern/qcustomplot/qcustomplot.cpp)
set_target_properties(gui PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(gui
        PUBLIC
        Qt5::Core Qt5::Widgets qcustomplot)

Never mind the verbose comments; my initial GUI training being in Java Swing , I found them useful.

EDIT: what helped me was the qt5_wrapper_cpp thing mentioned in Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs

Q_OBJECT macro requires code generation. That's why you got undefined reference exceptions. I don't remember exactly how to configure a cmake project for Qt, but I would recommend reading https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html .

Something like set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE) should help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM