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() with arguments
Forum Updated to NodeBB v4.3 + New Features

QMetaObject::invokeMethod() with arguments

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 7.0k 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.
  • btseB Offline
    btseB Offline
    btse
    wrote on last edited by
    #1

    Hi,

    I would like to use the Functor overload of the invokeMethod() functions since it does compile time checks that the function actually exists. However, how do I pass arguments using this method? Is it possible?

    raven-worxR 1 Reply Last reply
    0
    • btseB btse

      Hi,

      I would like to use the Functor overload of the invokeMethod() functions since it does compile time checks that the function actually exists. However, how do I pass arguments using this method? Is it possible?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @btse
      simply capture them in your lambda function?
      You would anyway specify them at the time you call invokeMethod

      QMetaObject::invokeMethod(obj, [var1,var2]() {
        ...
      });
      

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

      btseB 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @btse
        simply capture them in your lambda function?
        You would anyway specify them at the time you call invokeMethod

        QMetaObject::invokeMethod(obj, [var1,var2]() {
          ...
        });
        
        btseB Offline
        btseB Offline
        btse
        wrote on last edited by
        #3

        @raven-worx Thanks. I originally wanted to call a member function, but I guess I can wrap that call with a lambda function.

        Note that dest lives in a separate thread. Should this be thread safe?

        connect(
                   lineedit,
                   &QLineEdit::returnPressed,
                   [=]()
                   {
                      QString text = lineedit->text();
                      QMetaObject::invokeMethod(dest,
                                                [=]() 
                                                {
                                                   dest->setOutputFilePath(text);
                                                },
                                                Qt::QueuedConnection
                      );
                   }
        );
        
        raven-worxR 1 Reply Last reply
        0
        • btseB btse

          @raven-worx Thanks. I originally wanted to call a member function, but I guess I can wrap that call with a lambda function.

          Note that dest lives in a separate thread. Should this be thread safe?

          connect(
                     lineedit,
                     &QLineEdit::returnPressed,
                     [=]()
                     {
                        QString text = lineedit->text();
                        QMetaObject::invokeMethod(dest,
                                                  [=]() 
                                                  {
                                                     dest->setOutputFilePath(text);
                                                  },
                                                  Qt::QueuedConnection
                        );
                     }
          );
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @btse said in QMetaObject::invokeMethod() with arguments:

          Note that dest lives in a separate thread. Should this be thread safe?

          since you explicitly use QueuedConnection yes. You can also use AutoConnection (the default anyway) to let Qt decide.

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

          btseB 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @btse said in QMetaObject::invokeMethod() with arguments:

            Note that dest lives in a separate thread. Should this be thread safe?

            since you explicitly use QueuedConnection yes. You can also use AutoConnection (the default anyway) to let Qt decide.

            btseB Offline
            btseB Offline
            btse
            wrote on last edited by
            #5

            @raven-worx Yeah I guess the whole lambda capturing is something I still need to get comfortable with, especially when it's used in a multi-thread context.

            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