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.1k 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.
  • B Offline
    B Offline
    btse
    wrote on 27 Mar 2019, 17:18 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?

    R 1 Reply Last reply 27 Mar 2019, 17:27
    0
    • B btse
      27 Mar 2019, 17:18

      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?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 27 Mar 2019, 17:27 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

      B 1 Reply Last reply 27 Mar 2019, 17:53
      3
      • R raven-worx
        27 Mar 2019, 17:27

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

        QMetaObject::invokeMethod(obj, [var1,var2]() {
          ...
        });
        
        B Offline
        B Offline
        btse
        wrote on 27 Mar 2019, 17:53 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
                      );
                   }
        );
        
        R 1 Reply Last reply 27 Mar 2019, 18:07
        0
        • B btse
          27 Mar 2019, 17:53

          @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
                        );
                     }
          );
          
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 27 Mar 2019, 18:07 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

          B 1 Reply Last reply 27 Mar 2019, 18:25
          0
          • R raven-worx
            27 Mar 2019, 18:07

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

            B Offline
            B Offline
            btse
            wrote on 27 Mar 2019, 18:25 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

            1/5

            27 Mar 2019, 17:18

            • Login

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