Multiple inheritance of QObject
Unsolved
General and Desktop
-
I'm trying to create a class in which all top-level widgets of my application could inherit from it, so I could define all Q_PROPERTY that I would like to expose in just this class instead of declaring it in each different class.
I'm getting compilation errors about ambiguous ~ because QMainWindow already inherits QObject.
What i can do in this case?
class WindowProperty : public QObject { Q_OBJECT Q_PROPERTY(int test MEMBER test WRITE setTest) public: int test = 0; void setTest(int xtest) { qDebug() << "xtest:" << xtest; qDebug() << "test:" << test; } }; class MainWindow : public QMainWindow, public WindowProperty { Q_OBJECT public: }
-
-
It compiles just fine with Q_GADGET but it still will not do what you are expecting. The code generated by MOC is not heritable. (It's also in the WindowProperty private section where it would not be visible anyway. Changing that does not help.)
#include <QApplication> #include <QMainWindow> #include <QDebug> class WindowProperty { // not a QObject Q_GADGET Q_PROPERTY(int test MEMBER test WRITE setTest) public: int test = 0; void setTest(int xtest) { qDebug() << "xtest:" << xtest; qDebug() << "test:" << test; } }; class MainWindow : public QMainWindow, public WindowProperty { Q_OBJECT public: }; int main(int argc, char **argv) { QApplication app(argc, argv); MainWindow w; qDebug() << w.property("test"); // invalid variant, not 0 w.setProperty("test", 42); // sets a property in w's QObject qDebug() << w.property("test"); // 42 , not 0 return app.exec(); } #include "main.moc"