tags:

views:

120

answers:

2

Is it possible to tell the Qt MOC that I would like to declare the class and implement it in a single file rather than splitting them up into an .h and .cpp file?

+3  A: 

If you want to declare and implement a QObject subclass in you cpp file, you have to manually include the moc file.

For example: (file main.cpp)

struct SubObject : QObject
{
    Q_OBJECT
};

//...

#include "main.moc"

You have to rerun moc (make qmake) after adding the #include statement.

Job
I think you mean `qmake make` rather than the other way around? :)
Troubadour
No I don't. You only have to run `qmake` once to create you `Makefile` s. If you want to rerun moc afterwards, just run `make qmake`.
Job
Couldn't get it to work.. Seems like I'm out of luck. Thanks anyways.
ShiGon
What kind of errors are you getting?
Job
+1  A: 

I think you can normally declare and implement the class in the header file without using anything special, eg:

include

class MyClass : public QObject { Q_OBJECT

public: MyClass(QObject * parent) { // Constructor Content }

 methodExample()
 {
      // Method content
 }

};

After this you add the header file to the pri file and execute qmake again and that's it. You have a class that inherits from qobject and is implemented and declared int he .h file.

cnebrera
Thanks! That worked nicely. Just to be clear, I can't do forward declarations this way, right? I tried and got errors :-) I guess you can't have everything!
ShiGon
You are welcome! I think it is not possible since you have no cpp file to give the header for the forward declaration.
cnebrera