Q_OBJECT or not Q_OBJECT

前端 未结 2 824
独厮守ぢ
独厮守ぢ 2020-12-21 09:07

I wrote a little program with a my own class within the main.cpp. Here the code:

#include 
#include 
#include 

        
2条回答
  •  既然无缘
    2020-12-21 09:57

    Signals and slots in Qt are managed through the moc: meta object compiler. Basically, the moc generates additional C++ code for each class containing the Q_OBJECT macro in order to implement effectively the signals and slots mechanisms. The additional code is then linked to the original class declaration.

    The problem here is that your class is declared in main.cpp: this conflicts with how the moc is working with your code. You should declare your class in a separate header.

    More about the moc

    Edit: as hyde pointed, an alternative is to include in your cpp the file generated by the moc; more details

提交回复
热议问题