c++ - Q_OBJECT or not Q_OBJECT -


i wrote little program own class within main.cpp. here code:

#include <qapplication> #include <qpushbutton> #include <qlabel>  class mywidget : public qwidget {     //q_object public:     mywidget(qwidget* parent = 0);     qlabel* label;     qstring string;  signals: public slots:     void settextlabel();  };  void mywidget::settextlabel() {     label->settext("test"); }   mywidget::mywidget(qwidget* parent)       : qwidget(parent) {  }  int main(int argc, char** argv) {     qapplication app(argc, argv);      mywidget widget;     widget.show();      return app.exec(); } 

it seems work not "completely". slot doens't work. suppose have put q_object. but, doing so, got list of errors, this:

undefined reference `vtable mywidget' ........................................ collect2: error: ld returned 1 exit status make: *** [mywidget] error 1 

i can manage that? problem?

signals , slots in qt managed through moc: meta object compiler. basically, moc generates additional c++ code each class containing q_object macro in order implement signals , slots mechanisms. additional code linked original class declaration.

the problem here class declared in main.cpp: conflicts how moc working code. should declare class in separate header.

more moc

edit: hyde pointed, alternative include in cpp file generated moc; more details


Comments