PDA

View Full Version : Including Q_OBJECT in another macro



Thuan Seah Tan
14th March 2012, 04:07
Hi everyone,

I encountered an issue where visual studio complains the class declarations lacks Q_OBJECT macro.

The class definition looked something like this before:



class Abc : QObject
{
Q_OBJECT
MY_CUSTOM_MACRO(Abc)
public:
Abc();
};


Because we use the MY_CUSTOM_MACRO along with Q_OBJECT a lot, I have attempt to refactor the code so Q_OBJECT is now part of the MY_CUSTOM_MACRO. So I have changed definition of MY_CUSTOM_MACRO from this:



#define MY_CUSTOM_MACRO (E) \
public: \
void* operator new( size_t size ) \
{ \
void *storage = malloc(size); \
return storage; \
} \
private:


to this:



#define MY_CUSTOM_MACRO (E) \
Q_OBJECT \
public: \
void* operator new( size_t size ) \
{ \
void *storage = malloc(size); \
return storage; \
} \
private:


And the declaration of class Abc to this:



class Abc : QObject
{
MY_CUSTOM_MACRO(Abc)
public:
Abc();
};


This is when the error of missing Q_OBJECT macro starts popping up. Does moc or something scans the header file for Q_OBJECT without expanding MY_CUSTOM_MACRO or something and hence causing the error? Or is it something else I missed?

Thuan

ChrisW67
14th March 2012, 05:21
Does moc or something scans the header file for Q_OBJECT without expanding MY_CUSTOM_MACRO or something and hence causing the error?
Got it in one. moc does not include a complete C++ preprocessor or have access to the entire (compiler dependent) source code environment to allow this.