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. QMetaObject::invokeMethod absorbed somewhere?

QMetaObject::invokeMethod absorbed somewhere?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.0k Views 2 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.
  • P Offline
    P Offline
    pkuebler
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • P pkuebler

      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.

      P Offline
      P Offline
      pkuebler
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • P Offline
          P Offline
          pkuebler
          wrote on last edited by
          #4

          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

          kshegunovK 1 Reply Last reply
          0
          • P pkuebler

            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

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

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

            Read and abide by the Qt Code of Conduct

            P 1 Reply Last reply
            1
            • kshegunovK kshegunov

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

              P Offline
              P Offline
              pkuebler
              wrote on last edited by
              #6

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

              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