Is omitting a necessary Q_OBJECT always detected at build time?
-
Suppose I have a class which inherits from
QObject
, that this class uses modern slots (i.e., slots connected with the syntax introduced in Qt 5, not with macrosSIGNAL()
andSLOT()
), and that this class does not use any of Qt's meta-object features. Then, this class doesn't need to be declared withQ_OBJECT
.If I declare and emit a signal in that class, the build will fail if I forget to add
Q_OBJECT
to the declaration of the class.Are there any situations where omitting a necessary
Q_OBJECT
would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead? -
The Meta-Object System documentation provides a list of features:
- QObject::metaobject()
- QMetaObject::className()
- QObject::inherits()
- QObject::tr()
- QObject::setProperty() and getProperty()
- QMetaObject::newInstance()
- qobject_cast<>()
Without moc's output, these will behave as if called for the parent class.
-
Suppose I have a class which inherits from
QObject
, that this class uses modern slots (i.e., slots connected with the syntax introduced in Qt 5, not with macrosSIGNAL()
andSLOT()
), and that this class does not use any of Qt's meta-object features. Then, this class doesn't need to be declared withQ_OBJECT
.If I declare and emit a signal in that class, the build will fail if I forget to add
Q_OBJECT
to the declaration of the class.Are there any situations where omitting a necessary
Q_OBJECT
would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?@dave2 said in Is omitting a necessary Q_OBJECT always detected at build time?:
Are there any situations where omitting a necessary Q_OBJECT would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?
Would you like this to happen or why are you asking?
-
@dave2 said in Is omitting a necessary Q_OBJECT always detected at build time?:
Are there any situations where omitting a necessary Q_OBJECT would not be detected at build time (i.e., by the compiler or by the linker), but at run-time instead?
Would you like this to happen or why are you asking?
@Pl45m4 I would very much want erroneous omissions of
Q_OBJECT
to be detected by the build system rather than at run-time. Being confident that any erroneous omission ofQ_OBJECT
is detected by the build system would help me remove unnecessary uses of that macro. -
What's wrong with simply adding Q_OBJECT instead thinking about when it's maybe not needed (and you forget it by accident later on due to other, unrelated changes)?
-
What's wrong with simply adding Q_OBJECT instead thinking about when it's maybe not needed (and you forget it by accident later on due to other, unrelated changes)?
@Christian-Ehrlicher Our project contains about two thousand modules (= about one thousand h files + one thousand cpp files). About half of those modules define classes which inherit, directly or indirectly, QObject. They currently all use Q_OBJECT. Most of them (perhaps eight hundred), however, do not define signals, and thus presumably would not need Q_OBJECT. Removing eight hundred unnecessary instances of Q_OBJECT would mean that eight hundred mocs would not need to be generated, compiled, and linked.
Can you give one example where omitting a necessary Q_OBJECT would not be detected at build time, if such an example exists?
-
@Christian-Ehrlicher Our project contains about two thousand modules (= about one thousand h files + one thousand cpp files). About half of those modules define classes which inherit, directly or indirectly, QObject. They currently all use Q_OBJECT. Most of them (perhaps eight hundred), however, do not define signals, and thus presumably would not need Q_OBJECT. Removing eight hundred unnecessary instances of Q_OBJECT would mean that eight hundred mocs would not need to be generated, compiled, and linked.
Can you give one example where omitting a necessary Q_OBJECT would not be detected at build time, if such an example exists?
-
The Meta-Object System documentation provides a list of features:
- QObject::metaobject()
- QMetaObject::className()
- QObject::inherits()
- QObject::tr()
- QObject::setProperty() and getProperty()
- QMetaObject::newInstance()
- qobject_cast<>()
Without moc's output, these will behave as if called for the parent class.
-
The Meta-Object System documentation provides a list of features:
- QObject::metaobject()
- QMetaObject::className()
- QObject::inherits()
- QObject::tr()
- QObject::setProperty() and getProperty()
- QMetaObject::newInstance()
- qobject_cast<>()
Without moc's output, these will behave as if called for the parent class.
@jeremy_k You are right, thanks!
So, for example, you have a class,
MyClass
, which inheritsQObject
. IfMyClass
is declared withQ_OBJECT
,MyClass::staticMetaObject.className()
evaluates to"MyClass"
, whereas ifMyClass
is declared withoutQ_OBJECT
, the same expression evaluates to"QObject"
. Both versions build without error, but the one withoutQ_OBJECT
does not work as intended.Of the listed features, however, the only ones we use are
QObject::setProperty()
andQObject::property()
, but only in a very few places, and not on classes of our own (i.e., on classes of the Qt library). Therefore, we should be safe as far as these features are concerned.Please let us know if you can think of other examples.
-