I'm trying to use a macro to simplify Qt properties, so I don't need to manually define the reader and writer methods and the notify signal:
#define PROPERTY(type, name, reader, writer) \
public: \
Q_PROPERTY(type name READ reader WRITE writer NOTIFY name ## Changed) \
\
type reader() const { \
return m_ ## name; \
} \
\
public Q_SLOT: \
void writer(type name) { \
m_ ## name = name; \
emit name ## Changed(name); \
} \
\
private: \
type m_ ## name; \
\
Q_SIGNAL: \
void name ## Changed(type name);
Then I use it like:
class Test : public QObject
{
Q_OBJECT
PROPERTY(QString, name, name, setName)
}
However, I get errors during linking:
CMakeFiles/weather-desktop.dir/weather/location.o: In function `Weather::Location::setName(QString)':
/home/mspencer/Programs/weather-desktop/src/weather/location.h:37: undefined reference to `Weather::Location::nameChanged(QString)'
collect2: error: ld returned 1 exit status
make[2]: *** [src/weather-desktop] Error 1
make[1]: *** [src/CMakeFiles/weather-desktop.dir/all] Error 2
make: *** [all] Error 2
I think this is because Qt doesn't support multiple signals
sections, which is what results from using my macro. What is best way to write and use a macro to simplify Qt Properties?
Edit:
After looking at this question and the moc documentation, I think this is because moc doesn't expand #defines
. Is there any way to work around this?