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. How to Use Non Static function in QtConcurrent::run() Method
Forum Updated to NodeBB v4.3 + New Features

How to Use Non Static function in QtConcurrent::run() Method

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.4k Views 3 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.
  • K Offline
    K Offline
    Ketan__Patel__0011
    wrote on 21 Mar 2020, 13:48 last edited by
    #1

    I was Declare One Non Static Function Like:

    void MainWindow::Call_Me()
    {
    ui->label_1->setText("Welcome");
    }

    Now I Want to Use this Function In My PushButton ClickEvent Method

    void MainWindow::on_pushButton_clicked()
    {
    QFuture<void> future_1 = QtConcurrent::run(Call_Me());
    }

    Plzz Help Me

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 21 Mar 2020, 14:00 last edited by
      #2

      Why?!

      Whats wrong with

      void MainWindow::on_pushButton_clicked() {
          ui->label_1->setText("Welcome");
      }
      

      Or if you must call it as a function:

      void MainWindow::on_pushButton_clicked() {
          Call_Me();
      }
      

      You tell us nothing about why you think QFuture is necessary. Your GUI code is running in the main GUI thread, right?

      K 1 Reply Last reply 21 Mar 2020, 14:17
      0
      • K Kent-Dorfman
        21 Mar 2020, 14:00

        Why?!

        Whats wrong with

        void MainWindow::on_pushButton_clicked() {
            ui->label_1->setText("Welcome");
        }
        

        Or if you must call it as a function:

        void MainWindow::on_pushButton_clicked() {
            Call_Me();
        }
        

        You tell us nothing about why you think QFuture is necessary. Your GUI code is running in the main GUI thread, right?

        K Offline
        K Offline
        Ketan__Patel__0011
        wrote on 21 Mar 2020, 14:17 last edited by
        #3

        @Kent-Dorfman But I want To Run This Function Parallel

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 21 Mar 2020, 15:40 last edited by
          #4

          @Ketan__Patel__0011 said in How to Use Non Static function in QtConcurrent::run() Method:

          void MainWindow::on_pushButton_clicked()
          {
          QFuture<void> future_1 = QtConcurrent::run(Call_Me());
          }

          OK, so given your creation of QFuture as a local variable in the method, would you expect the result to be available after the method exits?

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 21 Mar 2020, 16:26 last edited by
            #5

            Hi,

            On a side note: don't modify GUI elements from other threads. Use signals and slots to transfer the data back to the GUI elements.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SimonSchroeder
              wrote on 23 Mar 2020, 07:28 last edited by
              #6

              You should note that Call_Me as a member method has an implicit parameter this which you need to pass. Out of the top of my head I would try something like this (though untested):

              ...QtConcurrent::run(std::mem_fn(&MainWindow::Call_Me), this);
              

              Usually, if you pass a function to another function it is actually a function pointer. So, you have to give the address of the function (hence the &). Also, member functions live in a class scope and you have to name the full scope. Finally, std::mem_fn helps to make the implicit argument the first argument of the function call.

              However, as said before GUI functions need to be executed in the GUI thread (which is basically the thread that main is running in). So, you can do computations in Call_Me, but you cannot call setText directly because label_1 is part of the GUI. I guess this is only a small example (and hopefully not what you are really trying to do) because the overhead of calling into a different thread is a lot worse than just executing a small portion of code (setText in this case).

              If you do have to call a GUI functions from a different thread, you actually have to queue them into the GUI thread. This would like something like this (again: untested):

              QMetaObject::invokeMethod(ui->label_1, SLOT(setText(QString())), Qt::QueuedConnection, Q_ARG(QString, "Welcome"))
              

              Maybe using a lambda instead is slightly easier:

              QMetaObject::invokeMethod(qApp, [this](){ ui->label_1->setText("Welcome"); }, Qt::QueuedConnection);
              
              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 23 Mar 2020, 07:49 last edited by
                #7

                Just in case, the documentation of QtConcurrent::run shows exactly how to call a member function on an object.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3

                1/7

                21 Mar 2020, 13:48

                • Login

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