April 7, 2012

ahura_24 ahura_24
Lab Rat
104 posts

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 ?

  1. #include <QtGui>
  2.  
  3. class label : public QLabel
  4. {
  5.     Q_OBJECT
  6. public:
  7.     label(QWidget *parent = 0)
  8.         : QLabel( parent )
  9.     { }
  10.  
  11. public:
  12.     void setText(const QString &txt)
  13.     {
  14.         QLabel::setText( txt );
  15.         emit valueChanged( txt.toInt() );
  16.     }
  17.  
  18. signals:
  19.     void valueChanged(int);
  20. };
  21.  
  22. class widget : public QWidget
  23. {
  24.     Q_OBJECT
  25. public slots:
  26.     void setValue(int t)
  27.     {
  28.         char buffer[100];
  29.         itoa(t, buffer, 10);
  30.  
  31.         setWindowTitle( buffer );
  32.     }
  33. };
  34.  
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38.     QApplication app(argc, argv);
  39.     widget window;
  40.  
  41.     label l( &window );
  42.     l.setText( "1" );
  43.     QObject::connect(&l, SIGNAL(valueChanged(int)), &window, SLOT(setValue(int)));
  44.     l.setText( "2" );
  45.  
  46.     window.show();
  47.     return app.exec();
  48. }

i have a base problem with signal/slot please help me ! tnx a lot

5 replies

April 8, 2012

mlong mlong
Robot Herder
1551 posts

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:

  1.     void setValue(int t)
  2.     {
  3.         QString buffer = QString::number(t);
  4.         setWindowTitle( buffer );
  5.     }

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

April 8, 2012

ahura_24 ahura_24
Lab Rat
104 posts

why i must seprate header files ?

April 8, 2012

koahnig koahnig
Gene Splicer
2905 posts

ahura_24 wrote:
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:

mlong wrote:
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.

April 9, 2012

Andre Andre
Robot Herder
6399 posts

Q_OBJECT, apart from being a trigger for the moc tool, also actualy declares a couple of methods on your object (that are mostly implemented by code generated by the moc tool) that are needed for the Qt meta object system and signals and slots to work properly.

April 9, 2012

1+1=10 1+1=10
Hobby Entomologist
643 posts
ahura_24 wrote:
why i must seprate header files ?

Not a must, but a good practice.

If you do not want to seprate and the name of this file is “main.cpp” and the build tool your chosen is “qmake” , you need to add such a line

  1. #include "main.moc"

in this file.

 
  ‹‹ bindTexture crash in release version      QPushButton over QGLWidget works wrong ››

You must log in to post a reply. Not a member yet? Register here!