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
2010-06-09 11:36:36
I think you mean `qmake make` rather than the other way around? :)
Troubadour
2010-06-09 11:49:20
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
2010-06-09 12:07:55
Couldn't get it to work.. Seems like I'm out of luck. Thanks anyways.
ShiGon
2010-06-12 21:15:05
What kind of errors are you getting?
Job
2010-06-12 22:13:46
+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
2010-06-13 09:37:56
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
2010-07-02 01:03:35
You are welcome! I think it is not possible since you have no cpp file to give the header for the forward declaration.
cnebrera
2010-07-02 10:57:05