Multiple inheritance of QObject
-
wrote on 12 Jul 2023, 19:00 last edited by
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: }
-
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: }
-
Why not include the properties instead of inherit from them?
"Making"QMainWindow
"a property" (-> inheritance = "is a") doesn't make too much sense IMOwrote on 12 Jul 2023, 20:11 last edited by Rua3n 7 Dec 2023, 20:13@Pl45m4 how to include Q_PROPERTIES?
What doesnt make sense? if you have 10 different classes that you want to share the same Q_PROPERTIEs, why keep track of defining all Q_PROPERTIES in each of these classes instead of just one? -
@Pl45m4 how to include Q_PROPERTIES?
What doesnt make sense? if you have 10 different classes that you want to share the same Q_PROPERTIEs, why keep track of defining all Q_PROPERTIES in each of these classes instead of just one?wrote on 12 Jul 2023, 23:10 last edited byIf you really want to do this, you could try to make
WindowProperty
aQ_GADGET
.
This doesn't allow signals and slots in there, since it's a light version ofQ_OBJECT
, but you can still useQ_PROPERTY
from the MOC system. -
If you really want to do this, you could try to make
WindowProperty
aQ_GADGET
.
This doesn't allow signals and slots in there, since it's a light version ofQ_OBJECT
, but you can still useQ_PROPERTY
from the MOC system. -
wrote on 13 Jul 2023, 03:36 last edited by
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"
-
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"
1/7