tags:

views:

96

answers:

3

If I derive my class from QObject (or a subclass), the Qt documentation says that I have to put the Q_OBJECT macro into my class declaration.

It also ways I need to "run the meta-object compiler" for my class.

I have no idea how to do this. Is this something I need to add to the .pro file? Do I need to edit the makefile? This seems overly complicated for a simple derived class.

I'm using Qt Creator.

EDIT:

Thanks for all the help. I discovered something very important: When I create a new class in Qt Creator, I have to specify "Inherits QWidget" as the type information, even if I'm already specifying a Qt widget as my base class (such as QTextEdit). It seems strange that Qt Creator couldn't figure that out on its own, but that seems to solve my signal/slot and moc problems.

+3  A: 

Qt uses the moc compiler to produce its meta-object system (which is required for introspecting classes and allowing for signals/slots, amongst other things).

By default, qmake will do the right thing when generating and compiling the moc_*.cpp files.

Yann Ramin
+1  A: 

The Q_OBJECT simply needs to be put in your class declaration, like this:

class MyObject : public QObject {
   Q_OBJECT

   // etc.
};
sth
+1  A: 

If your classes going to use signals and slots, use the macro Q_OBJECT. Else it is not necessary. If there is no signals and slots, just go on with your normal c++ way of using classes and all should work fine. You will not specifically go on and create moc_*.cpp files, but as "theatrus" said qmake will do that stuff for you. Hope it helps.

liaK
All QObject subclasses should always have Q_OBJECT. Otherwise stuff like the metaobject features won't work correctly.
Frank
@ Frank, oh is it?? Thanks... What are the all the metaobject features you were referring too?? I haven't tried any of those before
liaK