Invokable and pure virtual method(s)
-
Hello,
Is it allowed to have an invokable method in a class that doesn't extendQObject
? On a related note, do I need to mark all overrides as invokable, or declaring it for the pure virtual method in the base class is enough?
For example:class BaseClass { // ... Q_INVOKABLE virtual void someMethod() = 0; // < Is this valid at all? }; class DerivedClass : public BaseClass { // ... Q_INVOKABLE virtual void someMethod(); // < Is Q_INVOKABLE needed, provided the `BaseClass::someMethod()` is correctly defined? };
Kind regards.
-
base class is enough.
Since in the end the meta object calls the method as you would do, thus the same rules apply ;) -
@raven-worx
Thanks for clarifying that, I suspected as much. What about the first part of the question? Will themoc
be clever enough to recognize theQ_INVOKABLE
macro without theQ_OBJECT
meta-object information? I need only to schedule a queued call through the event loop, I won't need all theQObject
's bells and whistles? -
No, base class MUST BE inherits from QObject and QObject MUST BE declared in first place of base classes. In this case you can put Q_OBJECT macro into the class.
-
@Hamed.Masafi said:
No, base class MUST BE inherits from QObject and QObject MUST BE declared in first place of base classes. In this case you can put Q_OBJECT macro into the class.
that's simply not true!!
For such cases there is the Q_GADGET macro. -
@raven-worx
Superb, thank you! I didn't know about theQ_GADGET
macro, it must be new in Qt5. -
@kshegunov Nop, it's older than that ;) Q_GADGET dates back to Qt 4
-
There are lots of hidden gems to discover even after years of using Qt :)
-
@raven-worx @SGaist
Hello again,
Is it possible to have queued invocation on a gadget object? I can't seem to find such a thing. I'm currently retrieving the method itself by:QMetaObject & metaObject = AgDialPrivate::staticMetaObject; QMetaMethod scheduleChildAdd = metaObject.method(metaObject.indexOfMethod("scheduleChildAdd"));
however, it looks like
QMetaMethod::invokeOnGadget
doesn't acceptQt::ConnectionType
. Should I try outQ_PRIVATE_SLOT
instead?Kind regards.
-
@ttuna
Hello,
Thanks, I know that. I wanted to have a method of my private object to be queued for later execution, because theChildAdded
event that I'm handling in an event filter is propagated before the child object is fully constructed. It appears that the gadgets have no such capability so I've implemented the functionality as a private slot, and it works okay. For anyone that might be interested, here's how:class AGUI_API AgDial : public QStackedWidget { Q_OBJECT // ... private: Q_PRIVATE_SLOT(d(), void scheduleChildAdd(QPointer<QObject>)) };
With the corresponding invocation in the event filter:
bool AgDial::eventFilter(QObject * object, QEvent * event) { switch (event->type()) { case QEvent::ChildAdded: QMetaObject::invokeMethod(this, "scheduleChildAdd", Qt::QueuedConnection, Q_ARG(QPointer<QObject>, QPointer<QObject>(reinterpret_cast<QChildEvent *>(event)->child()))); break; // ... } }
Kind regards.