Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Invokable and pure virtual method(s)

    General and Desktop
    invokable virtual qobject
    5
    13
    7524
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • kshegunov
      kshegunov Moderators last edited by kshegunov

      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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        kshegunov 1 Reply Last reply Reply Quote 0
        • kshegunov
          kshegunov Moderators @raven-worx last edited by kshegunov

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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply Reply Quote 0
          • Hamed.Masafi
            Hamed.Masafi last edited by

            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.

            Remote object sharing (OO RPC)
            http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

            Advanced, Powerful and easy to use ORM for Qt5
            https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

            raven-worx 1 Reply Last reply Reply Quote -1
            • raven-worx
              raven-worx Moderators @Hamed.Masafi last edited by

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

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              kshegunov 1 Reply Last reply Reply Quote 1
              • kshegunov
                kshegunov Moderators @raven-worx last edited by kshegunov

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

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  kshegunov 1 Reply Last reply Reply Quote 0
                  • kshegunov
                    kshegunov Moderators @SGaist last edited by

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

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      kshegunov 1 Reply Last reply Reply Quote 1
                      • kshegunov
                        kshegunov Moderators @SGaist last edited by

                        @SGaist
                        I second that!

                        Read and abide by the Qt Code of Conduct

                        kshegunov 1 Reply Last reply Reply Quote 0
                        • kshegunov
                          kshegunov Moderators @kshegunov last edited by

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

                          Read and abide by the Qt Code of Conduct

                          T 1 Reply Last reply Reply Quote 0
                          • T
                            ttuna @kshegunov last edited by

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

                            kshegunov 1 Reply Last reply Reply Quote 0
                            • kshegunov
                              kshegunov Moderators @ttuna last edited by kshegunov

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

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post