Invokable and pure virtual method(s)


  • Qt Champions 2016

    Hello,
    Is it allowed to have an invokable method in a class that doesn't extend QObject? 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.


  • Moderators

    base class is enough.
    Since in the end the meta object calls the method as you would do, thus the same rules apply ;)


  • Qt Champions 2016

    @raven-worx
    Thanks for clarifying that, I suspected as much. What about the first part of the question? Will the moc be clever enough to recognize the Q_INVOKABLE macro without the Q_OBJECT meta-object information? I need only to schedule a queued call through the event loop, I won't need all the QObject'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.


  • Moderators

    @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.


  • Qt Champions 2016

    @raven-worx
    Superb, thank you! I didn't know about the Q_GADGET macro, it must be new in Qt5.


  • Lifetime Qt Champion

    @kshegunov Nop, it's older than that ;) Q_GADGET dates back to Qt 4


  • Qt Champions 2016

    @SGaist
    Huh, thanks for the clarification. It's possible, although I never knew. Always something new to see, always something new to learn, I guess ... :)


  • Lifetime Qt Champion

    There are lots of hidden gems to discover even after years of using Qt :)


  • Qt Champions 2016

    @SGaist
    I second that!


  • Qt Champions 2016

    @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 accept Qt::ConnectionType. Should I try out Q_PRIVATE_SLOT instead?

    Kind regards.



  • @kshegunov
    AFAIK gadgets doesn't support signal / slot therefore only a direct call is possible.


  • Qt Champions 2016

    @ttuna
    Hello,
    Thanks, I know that. I wanted to have a method of my private object to be queued for later execution, because the ChildAdded 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.