PDA

View Full Version : When do I need Q_OBJECT?



Morea
24th February 2006, 08:54
Hi.
As I understand it, I need Q_OBJECT macro if I declare a class that should use signals and slots, and it should then also be derived from QObject?
Like

class Foo:public QObject{
Q_OBJECT
// etc.
}

But if I derive from a class that already is derived from a QObject, like


class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget * parent = 0);
private:
Ui::MainWindow ui;
};


Is Q_OBJECT really needed here? I mean, there is nothing that uses signals/slots, the MainWindow constructor contains no signals emits or connects.
I mean, the QMainWindow class is derived from QObject, doesn't it get the Q_OBJECT for that part?

Everall
24th February 2006, 09:15
Is Q_OBJECT really needed here? I mean, there is nothing that uses signals/slots, the MainWindow constructor contains no signals emits or connects.
I mean, the QMainWindow class is derived from QObject, doesn't it get the Q_OBJECT for that part?
You are right. The signals and slots from QMainWindow are available without using Q_OBJECT in your derived class.

If you wanted to use new signal-slots in your derived class you would need Q_OBJECT. In your case you don't.


have a look at this similar thread.
http://www.qtcentre.org/forum/showthread.php?t=532&highlight=q_object