Invokable and pure virtual method(s)
-
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.