moc does not run if Q_OBJECT macro defined in another macro
-
I am attempting to write some code which can be easily ported away from using Qt. To do so, I am wrapping the Q_OBJECT macro inside my own macro as so
#define MY_OBJECT(...) Q_OBJECT using MyObject = QObject;
Which gets used as follows:
class Example : public MyObject { MY_OBJECT(Example) };
If I run the moc manually on this file, everything works well. However, when I attempt to compile this code using Qt Creator, it does not event attempt to run the moc for this file. And thus the compilation fails due to missing function definitions.
What do I need to do to get this to work?
-
Hi,
Did you force a re-run of qmake after changing your header ?
-
Yes. I even tried deleting the entire build folder an re-generating it. I looked at the "Compile Output" logs and I do not see the moc being called for the file at all.
As a comparison, I simple added Q_OBJECT to one of the files which was using MY_OBJECT. Once I did this, the moc_*.cpp file was generated properly.
I am using Qt creator 6.0.1 with Qt 5.15.2 if that helps.
-
After some reflection, I am wondering whether moc can expand your macro properly.
I remember there was a limitation related to macro expansion that was lifted for Qt 5 but I don't remember whether it applied to your case.
-
If that's the case, since I am only really using signals in the code I want to be "portable", is there a way to generate the signal boilerplate code without using the Q_OBJECT macro (and thus without using the moc)?
-
Yes, there's Woboq's Verdigris.
-
QtCreator is just a wrapper around running the relevant build tools. Are you using qmake or CMake in your QtCreator build kit?
If it is qmake, is the relevant header file in qmake's HEADERS variable?
If CMake, perhaps AUTOMOC is not working because it does not see the magic "Q_OBJECT". The docs seem to describe exactly the sort of "hidden" Q_OBJECT scenario you have. You can define the AUTOMOC_MACRO_NAMES variable to tweak what it looks for.
https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html#automoc