Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Queued invoke on virtual slot in constructor
Forum Updated to NodeBB v4.3 + New Features

Queued invoke on virtual slot in constructor

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.7k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    Good or evil?

    For several of my objects, I need to perform some initial updates asynchronously, after the constructor has already run.
    The objects share a base class, and to ensure that no derived class forgets about the asynchronous initialization step, I do like this:

    @
    class AbstractBase : public QObject
    {
    Q_OBJECT
    public:
    AbstractBase(QObject* parent = 0)
    : QObject(parent)
    {
    QMetaObject::invokeMethod(this, "doInitialUpdate", Qt::QueuedConnection);
    }

    protected slots:
    virtual void doInitialUpdate() = 0;
    };
    @

    The questions is:
    Will Qt resolve the function pointer synchronously (within the constructor), thereby calling the pure virtual function of AbstractBase?
    Or will it rather wait until the event queue ran, and then resolve the function name to the slot of the correct derived class?
    Is this behavior specified and can I depend on it?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Why don't you try and see? A simple debug statement in your re-implemented doInitialUpdate() methods will tell you exactly what is happening...

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        There is a difference between "it works now, maybe by chance, maybe depending on Qt version/compiler/OS/whatnot" and "it is supposed to work according to standard/documentation/etc."

        I could however look into the Qt source code, that's and option.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Ok, point taken.

          It will work. Line 8 in your code above will result in an event being posted in the event loop. The object for that will be a QObject, I think, but certainly not AbtractBase or whatever is derived of that. Then, the event is taken of the event queue again the next time the queue is processed, and translated back to a slot invocation. Because your slot is a virtual method, only at the invokation of the method it will be decided (by the C++ mechanism for that) which method to call.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            Thank you, Andre.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved