Why won't this compile (link) with the Q_OBJECT macro in place?

后端 未结 2 864
粉色の甜心
粉色の甜心 2021-01-14 13:07

I made a prototype of a project with PyQt and made it work there, now I\'m trying to convert it to C++ and am having some problems.

If I don\'t put the Q_OBJECT macr

相关标签:
2条回答
  • 2021-01-14 13:45

    This kind of errors usually happen when you add the Q_OBJECT macro and forget to rerun moc. If you use qmake, just run make qmake after you added the macro.

    As for your second question: you won't be able to use signals/slots (among other things) without the Q_OBJECT macro. See the docs for more information about this.

    0 讨论(0)
  • 2021-01-14 13:55

    Recently I tried to compile QDeviceWatcher on Linux and I get the same error WRT QDeviceWatcherPrivate class, which declaration can be found in qdevicewatcher_p.h and definition in qdevicewatcher_linux.cpp (on Linux).

    I use cmake as build system and my CMakeLists.txt looks like:

    cmake_minimum_required(VERSION 3.8)
    
    project("QDeviceWatcher" LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOMOC ON)
    
    find_package(Qt5 REQUIRED COMPONENTS Core Network)
    
    set(SOURCES)
    list(APPEND SOURCES "qdevicewatcher.cpp")
    if(WINCE)
        list(APPEND SOURCES "qdevicewatcher_wince.cpp")
    elseif(WIN32)
        list(APPEND SOURCES "qdevicewatcher_win32.cpp")
    elseif(APPLE)
        list(APPEND SOURCES "qdevicewatcher_mac.cpp")
    elseif(UNIX)
        list(APPEND SOURCES "qdevicewatcher_linux.cpp")
    else()
        message(FATAL_ERROR "no supported platform detected")
    endif()
    
    add_library(${PROJECT_NAME} STATIC ${SOURCES})
    target_include_directories(${PROJECT_NAME} PUBLIC ".")
    
    target_link_libraries(${PROJECT_NAME} PUBLIC Qt5::Core Qt5::Network)
    
    set_target_properties(${PROJECT_NAME} PROPERTIES
        CXX_STANDARD 17
        CXX_STANDARD_REQUIRED YES
        CXX_EXTENSIONS YES
        )
    

    As you can see in add_library I only provide *.cpp files. I suspect, that cmake runs Meta object compiler and looks through all the sources, maybe except #include dependencies, which filenames (w/o extension) not match *.cpp's filenames (just assumption), for the QObject/QWidget/... bases and Q_OBJECT macro. And I think cmake missed qdevicewatcher_p.h to look through and run MOC against it.

    After I added the "qdevicewatcher_p.h" to the list of sources the error is ceased to exist.

    0 讨论(0)
提交回复
热议问题