QMetaObject::invokeMethod absorbed somewhere?



  • Hi there,

    I'm currently faced with a strange behaviour. I've got an object's function that has to be called from the main thread. I therefore use QMetaObject::invokeMethod in order to hand over the function call to the main thread. My code looks like that:

    QMetaObject::invokeMethod(this, "MyMethod", Qt::QueuedConnection, Q_ARG(MyParameter,para));
    

    The function itself is declared as public slot:

        public slots:
            virtual void UpdateAllQGraphicsItems(MyParameter para);
    

    Within my test environment I have many object instances of the same type. All of them use the invokeMethod call. However, the call does not work in some circumstances (inexplicably). It almost works. And even if it not works, the return value of invokeMethod is anyway true?

    Are there some conditions where invoke-calls may be blocked or discarded?

    Any help would be appreciated.



  • There was a mistake in my second snippet. My second snippet should look like this:

        public slots:
            virtual void MyMethod(MyParameter para);
    

    Otherwise the invoke would fail in any condition. But - to avoid misunderstanding - when having the right function names the invoke calls sometimes gets discarded.


  • Qt Champions 2016

    Hello,

    However, the call does not work in some circumstances (inexplicably).

    Please describe those circumstances, or at least try to speculate on them, otherwise it's really hard to give any decent advice.

    It almost works.

    What does "almost" mean in this case?

    And even if it not works, the return value of invokeMethod is anyway true?

    The return value is irrelevant here. You're using a deferred function call, which means you can't get any (meaningful) return value from that, as the call is/will be made sometime in the future.

    Are there some conditions where invoke-calls may be blocked or discarded?

    No, not to my knowledge, except perhaps that there isn't a matching method. Also you should make sure your argument is registered with the metatype system, otherwise you can't use it with queued connections.

    Kind regards.



  • Hi,

    first of all thanks for you advices.

    Here some more details on the circumstances.

    I've got multiple instances of one and the same object type. Each instance has an unique object id and a set of 0 .. n QGraphicsItems:

    MyObject* pObInstance | GetObjectID() -> 1
    MyObject* pObInstance | GetObjectID() -> 2
    MyObject* pObInstance | GetObjectID() -> ..
    MyObject* pObInstance | GetObjectID() -> n
    

    The object defines a public slot that is to be called from within main thread in order to work with the QGraphicsItems:

        public slots:
            virtual void MyMethod(MyParameter para);
    

    Within MyMethod there is a check for the thread belonging. If a call does not have the main thread as context the call is being redirected via invokeMethod (the parameter is getting registered via qRegisterMetaType):

    if(QThread::currentThread() != QApplication::instance()->thread())
    {
       QMetaObject::invokeMethod(this, "MyMethod", Qt::QueuedConnection, Q_ARG(MyParameter,para));
       return;
    }
    

    The phenomenon is that some of the calls does not get invoked. However, most of them work. In detail: let's say we have 10 instances getting a call to MyMethod i.e. from within a loop consecutively.

    void MyObject::MyMethod(MyParameter para)
    {
       TRACE("%d called\n", GetObjectID());
    
       if(QThread::currentThread() != QApplication::instance()->thread())
       {
          TRACE("%d call redirected\n", GetObjectID());
          bool b = QMetaObject::invokeMethod(this, "MyMethod", Qt::QueuedConnection, Q_ARG(MyParameter,para));
          return;
       }
    
       TRACE("%d called from main thread\n", GetObjectID());
    
       /*
       do the work
       ...
       ...
       ...
       */
    }
    

    In the error circumstance the redirection of instance X is getting invoked. invokeMethod returns true. However, the call in the main thread context is not getting executed. Strange, isn't it?

    Best regards,
    Peter


  • Qt Champions 2016

    @pkuebler

    May I ask why are you doing those shenanigans?

    Within MyMethod there is a check for the thread belonging.

    For what purpose? QMetaObject::invokeMethod does that when used with Qt::AutoConnection and your comparison code:

    if(QThread::currentThread() != QApplication::instance()->thread())
    

    is not thread-safe by any measure.

    In detail: let's say we have 10 instances getting a call to MyMethod i.e. from within a loop consecutively.

    Just invoke the method with QMetaObject::invokeMethod from everywhere:

    void MyObject::MyMethod(MyParameter para)
    {
       // There's no real purpose to the thread checks (and they aren't really thread-safe as I mentioned). 
       /*
       do the work
       ...
       ...
       ...
       */
    }
    

    Then use invokeMethod and leave Qt to deal with what object is in what thread:

    QMetaObject::invokeMethod(objectInstace, "MyMethod", Qt::AutoConnection, Q_ARG(MyParameter, para));
    

    Kind regards.



  • @kshegunov

    Thanks for your advice :-) AutoConnection works

    The check for the thread context came in because we use it in the .Net environment that way. Good to know that there is a better solution on the Qt side.


Log in to reply
 

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