Interfaces must inherit QObject?!
-
Given a couple of classes such as:
#include <QObject> class ICake { public: ICake(){} }; class VictoriaSpongeCake : public ICake, public QObject { Q_OBJECT public: VictoriaSpongeCake():ICake(){} };
why do I get errors such as:
/home/j/develop/qt/projects/build-InheritInterface-Desktop_Qt_5_15_2_GCC_64bit-Debug/moc_victoriaspongecake.cpp:67: error: ‘staticMetaObject’ is not a member of ‘ICake’ moc_victoriaspongecake.cpp:67:41: error: ‘staticMetaObject’ is not a member of ‘ICake’ 67 | QMetaObject::SuperData::link<ICake::staticMetaObject>(), | ^~~~~~~~~~~~~~~~
Of course the intention here is for concrete 'cakes' (errhum...sorry) to implement some signals & slots.
So if I change ICake to inherit like this:
class ICake: public QObject
It all compiles without a problem.
-
Given a couple of classes such as:
#include <QObject> class ICake { public: ICake(){} }; class VictoriaSpongeCake : public ICake, public QObject { Q_OBJECT public: VictoriaSpongeCake():ICake(){} };
why do I get errors such as:
/home/j/develop/qt/projects/build-InheritInterface-Desktop_Qt_5_15_2_GCC_64bit-Debug/moc_victoriaspongecake.cpp:67: error: ‘staticMetaObject’ is not a member of ‘ICake’ moc_victoriaspongecake.cpp:67:41: error: ‘staticMetaObject’ is not a member of ‘ICake’ 67 | QMetaObject::SuperData::link<ICake::staticMetaObject>(), | ^~~~~~~~~~~~~~~~
Of course the intention here is for concrete 'cakes' (errhum...sorry) to implement some signals & slots.
So if I change ICake to inherit like this:
class ICake: public QObject
It all compiles without a problem.
@idlefrog said in Interfaces must inherit QObject?!:
Of course the intention here is for concrete 'cakes' (errhum...sorry) I want to implement some signals & slots.
I think
QObject
should be the first class in inheritance list:class VictoriaSpongeCake : public QObject, public ICake { Q_OBJECT public: VictoriaSpongeCake(QObject *parent = nullptr):QObject(parent), ICake(){} };
-
Given a couple of classes such as:
#include <QObject> class ICake { public: ICake(){} }; class VictoriaSpongeCake : public ICake, public QObject { Q_OBJECT public: VictoriaSpongeCake():ICake(){} };
why do I get errors such as:
/home/j/develop/qt/projects/build-InheritInterface-Desktop_Qt_5_15_2_GCC_64bit-Debug/moc_victoriaspongecake.cpp:67: error: ‘staticMetaObject’ is not a member of ‘ICake’ moc_victoriaspongecake.cpp:67:41: error: ‘staticMetaObject’ is not a member of ‘ICake’ 67 | QMetaObject::SuperData::link<ICake::staticMetaObject>(), | ^~~~~~~~~~~~~~~~
Of course the intention here is for concrete 'cakes' (errhum...sorry) to implement some signals & slots.
So if I change ICake to inherit like this:
class ICake: public QObject
It all compiles without a problem.
@idlefrog said in Interfaces must inherit QObject?!:
It all compiles without a problem.
I am surprised. Maybe you will have runtime problems. You will find plenty of articles out there telling you in Qt you must not inherit from
QObject
multiple times. -
@idlefrog said in Interfaces must inherit QObject?!:
Of course the intention here is for concrete 'cakes' (errhum...sorry) I want to implement some signals & slots.
I think
QObject
should be the first class in inheritance list:class VictoriaSpongeCake : public QObject, public ICake { Q_OBJECT public: VictoriaSpongeCake(QObject *parent = nullptr):QObject(parent), ICake(){} };
@KroMignon Yes,.. that did the job! Wouldn't have thought the inheritance order would matter... clearly it does. Thanks!
-
@idlefrog said in Interfaces must inherit QObject?!:
It all compiles without a problem.
I am surprised. Maybe you will have runtime problems. You will find plenty of articles out there telling you in Qt you must not inherit from
QObject
multiple times. -
@JonB Yes, I also thought I would end up (eventually) in a catch 22 situation with diamond inheritance all over the shop! Thank goodness a simple inheritance reordering did the trick! :)
@idlefrog said in Interfaces must inherit QObject?!:
Thank goodness a simple inheritance reordering did the trick! :)
I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.
-
@idlefrog said in Interfaces must inherit QObject?!:
Thank goodness a simple inheritance reordering did the trick! :)
I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.
@KroMignon
I should like a reference for this. So far as I know multiple inheritance fromQObject
is not supported. See for example QObject Multiple Inheritance. This is required by moc, perhaps we will see where the OP gets over time.The suggested solution there shows "the Qt way" of implementing what I think @idlefrog is trying to achieve, avoiding the multiple inheritance.
-
@KroMignon
I should like a reference for this. So far as I know multiple inheritance fromQObject
is not supported. See for example QObject Multiple Inheritance. This is required by moc, perhaps we will see where the OP gets over time.The suggested solution there shows "the Qt way" of implementing what I think @idlefrog is trying to achieve, avoiding the multiple inheritance.
-
@KroMignon
I should like a reference for this. So far as I know multiple inheritance fromQObject
is not supported. See for example QObject Multiple Inheritance. This is required by moc, perhaps we will see where the OP gets over time.The suggested solution there shows "the Qt way" of implementing what I think @idlefrog is trying to achieve, avoiding the multiple inheritance.
@KroMignon said in Interfaces must inherit QObject?!:
I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.
...
@JonB said in Interfaces must inherit QObject?!:
I should like a reference for this.
https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first
-
@KroMignon said in Interfaces must inherit QObject?!:
I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.
...
@JonB said in Interfaces must inherit QObject?!:
I should like a reference for this.
https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first
@JKSH said in Interfaces must inherit QObject?!:
https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first
If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
Thank you for this reference. However, unless I read it wrong, this does not address the @idlefrog's case or @KroMignon's solution.
In the example in the docs
class SomeClass : public QObject, public OtherClass
Here there is no indication that the second class,
OtherClass
, itself inheritsQObject
. And it states explicitlyAlso, be sure that only the first inherited class is a
QObject
.@idlefrog is asking specifically about multiple inheritance where both classes come from
QObject
, and that is what i was trying to answer.I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve
QObject
is not supported, regardless of ordering? -
@JKSH said in Interfaces must inherit QObject?!:
https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first
If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
Thank you for this reference. However, unless I read it wrong, this does not address the @idlefrog's case or @KroMignon's solution.
In the example in the docs
class SomeClass : public QObject, public OtherClass
Here there is no indication that the second class,
OtherClass
, itself inheritsQObject
. And it states explicitlyAlso, be sure that only the first inherited class is a
QObject
.@idlefrog is asking specifically about multiple inheritance where both classes come from
QObject
, and that is what i was trying to answer.I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve
QObject
is not supported, regardless of ordering?@JonB said in Interfaces must inherit QObject?!:
I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?
I up-voted @JKSH post, because it gives the link to documentation I talking about.
But, the reply to your question: according to documentationQObject
multiple inheritance (diamond inheritance) is not allowed/possible. -
@JonB said in Interfaces must inherit QObject?!:
I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?
I up-voted @JKSH post, because it gives the link to documentation I talking about.
But, the reply to your question: according to documentationQObject
multiple inheritance (diamond inheritance) is not allowed/possible.@KroMignon Thanks @Kro, understood, I'm just seeking clarification because this is all getting a bit muddy.... :)
-
It seems to me that for a class where you inherited perhaps a set of interfaces and one QObject, you can't inherit any other QObject class. e.g.
class RealCake: public QObject, public ICake, public IEdible{} class PlasticCake: public QObject, public ICake {} class JokeCake: public PlasticCake, public RealCake{}
The
JokeCake
is not allowed in the world of Qt. Pity! -
@JKSH said in Interfaces must inherit QObject?!:
https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first
If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
Thank you for this reference. However, unless I read it wrong, this does not address the @idlefrog's case or @KroMignon's solution.
In the example in the docs
class SomeClass : public QObject, public OtherClass
Here there is no indication that the second class,
OtherClass
, itself inheritsQObject
. And it states explicitlyAlso, be sure that only the first inherited class is a
QObject
.@idlefrog is asking specifically about multiple inheritance where both classes come from
QObject
, and that is what i was trying to answer.I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve
QObject
is not supported, regardless of ordering?@JonB said in Interfaces must inherit QObject?!:
I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?
If both classes inherit from
QObject
you enter virtual-inheritance-land, where the night is dark and full of terrors ... and it's simply not supported by moc (last sentence of the mentioned link). Otherwise multiple inheritance is fine, good, expected and so on (provided you use it properly), and is supported both by the language and by Qt with the minor note about base class order. -
It seems to me that for a class where you inherited perhaps a set of interfaces and one QObject, you can't inherit any other QObject class. e.g.
class RealCake: public QObject, public ICake, public IEdible{} class PlasticCake: public QObject, public ICake {} class JokeCake: public PlasticCake, public RealCake{}
The
JokeCake
is not allowed in the world of Qt. Pity!@idlefrog said in Interfaces must inherit QObject?!:
The JokeCake is not allowed in the world of Qt. Pity!
Even if it were allowed, you're using it incorrectly. C++ is hard, man, it's just the way it is.
-
It seems to me that for a class where you inherited perhaps a set of interfaces and one QObject, you can't inherit any other QObject class. e.g.
class RealCake: public QObject, public ICake, public IEdible{} class PlasticCake: public QObject, public ICake {} class JokeCake: public PlasticCake, public RealCake{}
The
JokeCake
is not allowed in the world of Qt. Pity!@idlefrog
Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times fromQObejct
, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes
I am amazed that this actually works, but bummed out by the fact that it requires the old-style SIGNAL()/SLOT() syntax...
[Not my language!] Which is a bu**er :(
-
@idlefrog
Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times fromQObejct
, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes
I am amazed that this actually works, but bummed out by the fact that it requires the old-style SIGNAL()/SLOT() syntax...
[Not my language!] Which is a bu**er :(
@JonB said in Interfaces must inherit QObject?!:
I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes
This is simply an abomination. Signals act polymorphically without the need to declare them as pure virtual in interfaces. Slots are a different kettle of fish, they work just fine with overriding.
(Edit)
Addendum:Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times from QObejct, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.
Just to spill it as explicitly and bluntly as I possibly can:
You shall not derive a class from the same base multiple times (directly or indirectly) if you don't intimately know what's the difference between:class Derived : public Base
and
class Derived : virtual public Base
-
@JonB said in Interfaces must inherit QObject?!:
I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes
This is simply an abomination. Signals act polymorphically without the need to declare them as pure virtual in interfaces. Slots are a different kettle of fish, they work just fine with overriding.
(Edit)
Addendum:Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times from QObejct, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.
Just to spill it as explicitly and bluntly as I possibly can:
You shall not derive a class from the same base multiple times (directly or indirectly) if you don't intimately know what's the difference between:class Derived : public Base
and
class Derived : virtual public Base
@kshegunov
Yeah, I didn't notice thevirtual
on the signal. That's the trouble with assuming an accepted solution with 60 up-votes must be good!It shows a lot of people are obviously struggling with this. How then can the OP here arrange to write an "interface" which inherits from
QObject
and apply it to classes which inherit fromQObject
too?Ohhhh, maybe I misunderstood? Only now do I see that OP did not originally say that
ICake
neededQObject
itself. Only that he put it first in the class inheritance list beforeQObject
and that upset moc. I get it now, and whypublic QObject, public ICake
works here. OK, but what about if OP did want multipleQObject
inheritance, any way without the "abomination" ? -
@kshegunov
Yeah, I didn't notice thevirtual
on the signal. That's the trouble with assuming an accepted solution with 60 up-votes must be good!It shows a lot of people are obviously struggling with this. How then can the OP here arrange to write an "interface" which inherits from
QObject
and apply it to classes which inherit fromQObject
too?Ohhhh, maybe I misunderstood? Only now do I see that OP did not originally say that
ICake
neededQObject
itself. Only that he put it first in the class inheritance list beforeQObject
and that upset moc. I get it now, and whypublic QObject, public ICake
works here. OK, but what about if OP did want multipleQObject
inheritance, any way without the "abomination" ?@JonB said in Interfaces must inherit QObject?!:
OK, but what about if OP did want multiple QObject inheritnace, any way without the "abomination" ?
He shan't do it. Also read the addendum I added to my previous post.
-
@JonB said in Interfaces must inherit QObject?!:
OK, but what about if OP did want multiple QObject inheritnace, any way without the "abomination" ?
He shan't do it. Also read the addendum I added to my previous post.
@kshegunov
Is that a requirement of C++? The OP claimedclass VictoriaSpongeCake : public ICake, public QObject
So if I change
ICake
to inherit like this:class ICake: public QObject
It all compiles without a problem.
That is not quoting me, it is quoting him.