q_object , signals , slots problem
hi everybody . im beginner in qt. i have some question. “exactly” what is Q_OBJECT and where i must write this macro and what does it do ?
what is my problem in this code ?
- #include <QtGui>
- {
- Q_OBJECT
- public:
- { }
- public:
- {
- emit valueChanged( txt.toInt() );
- }
- signals:
- void valueChanged(int);
- };
- {
- Q_OBJECT
- public slots:
- void setValue(int t)
- {
- char buffer[100];
- itoa(t, buffer, 10);
- setWindowTitle( buffer );
- }
- };
- int main(int argc, char *argv[])
- {
- widget window;
- label l( &window );
- l.setText( "1" );
- l.setText( "2" );
- window.show();
- return app.exec();
- }
i have a base problem with signal/slot please help me ! tnx a lot
5 replies
Break your widget and label classes into their own files. It just works much better that way. Otherwise there is #include magic you have to do manually to include and link files that moc generates.
The Q_OBJECT macro instructs the moc preprocessor to generate the code necessary to implement the signals and slots. It must appear in the private section of a class definition.
Also, in your widget::setValue(int) method, use Qt’s native string functions, rather than C-based code:
why i must seprate header files ?
Your program is rather small at the time. So it might not be logic that you, but it is good practice. The reason is that it helps you to organize your code in a better way.
in you particular issue mlong has provided the reason:
Break your widget and label classes into their own files. It just works much better that way. Otherwise there is #include magic you have to do manually to include and link files that moc generates.
@
This should help you to get around some hurdles more easily.
You must log in to post a reply. Not a member yet? Register here!




