I am trying to simplify (i.e. get rid of the loads of template code) creating QObject wrapper classes that redirect access to the properties of other derived QObject classes.
To start small, I'm just trying to use it with one property:
// Sy_test.h - The wrapped class class Sy_test : public QObject { Q_OBJECT Q_PROPERTY( bool prop READ getProp WRITE setProp NOTIFY propChanged ) public: Sy_test( QObject* parent = nullptr ) : QObject{ parent }, prop_{ false } {} bool getProp() const { return prop_; } public slots: void setProp( bool value ) { if ( value != prop_ ) { prop_ = value; emit propChanged( prop_ ); } } signals: void propChanged( bool value ); private: bool prop_; }; // Sy_proxy.h - The wrapper generator
So, the SY_PROXY macro should create a class called Sy_testProxy that carries a copy of the Sy_test::prop property with implementations that just send requests / signals.
And it is almost so. Looking at the output of the postprocessor (I use g ++, so the .ii files), I see that the Sy_testProxy class Sy_testProxy built and has the same shape as the Sy_test class. However, I get the error message:
../CppTest/Sy_proxy.h:47: Error: NOTIFY signal 'propChanged' of property 'prop' does not exist in class Sy_testProxy. make: *** [moc_Sy_proxy.cpp] Error 1
So it seems that moc not parsing my macromagy; although I'm not sure how to clearly present macro SY_PROXY (the error comes from a class called Sy_testProxy ), as well as SY_PROXYPROPERTY (since moc must read Q_PROPERTY macro from it). Can anyone see where I made a mistake?
For the record: I hate macros, like everyone else, but I ended up using them because of moc aversion to templates and QObject virtual inheritance. This study was initiated because I had a collection of instances performing heavy computations in a separate thread, but they controlled the QML views. However, QML does not allow binding of links / properties to objects outside the main thread, so I was forced to create a proxy object that lives in the main thread. If anyone has a better idea, I am very open to them!
source share