Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] Running function with QtConcurrent::run

[Solved] Running function with QtConcurrent::run

Scheduled Pinned Locked Moved Mobile and Embedded
threadfunctionrunconcurrent
9 Posts 3 Posters 10.6k Views
  • 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.
  • M Offline
    M Offline
    McLion
    wrote on 23 Sept 2015, 17:19 last edited by McLion 10 Jan 2015, 07:44
    #1

    Hi

    I have a function
    char QTGUI_MainWindow::FormatSDcardForLivePause(void)
    that I need to run in a separate thread and catch when it has finished without blocking the rest of the application.
    I am trying as follows:

    QFutureWatcher<char> watcher;
    connect(&watcher, SIGNAL(finished()), this, SLOT(handleFinished()));
    QFuture<char> future = QtConcurrent::run(FormatSDcardForLivePause);
    watcher.setFuture(future);
    

    According: http://doc.qt.io/qt-4.8/qtconcurrentrun.html
    I get a
    no matching function for call to 'run(<unresolved overload function type>)

    What am I doing wrong?
    Thanks
    McL

    J 1 Reply Last reply 24 Sept 2015, 00:36
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Sept 2015, 20:14 last edited by
      #2

      Hi,

      You function belongs to QTGUI_MainWindow, see QtConcurrentRun using member functions

      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
      • M McLion
        23 Sept 2015, 17:19

        Hi

        I have a function
        char QTGUI_MainWindow::FormatSDcardForLivePause(void)
        that I need to run in a separate thread and catch when it has finished without blocking the rest of the application.
        I am trying as follows:

        QFutureWatcher<char> watcher;
        connect(&watcher, SIGNAL(finished()), this, SLOT(handleFinished()));
        QFuture<char> future = QtConcurrent::run(FormatSDcardForLivePause);
        watcher.setFuture(future);
        

        According: http://doc.qt.io/qt-4.8/qtconcurrentrun.html
        I get a
        no matching function for call to 'run(<unresolved overload function type>)

        What am I doing wrong?
        Thanks
        McL

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 24 Sept 2015, 00:36 last edited by
        #3

        @McLion said:

        I get a
        no matching function for call to 'run(<unresolved overload function type>)

        See the documentation on how to use QtConcurrent::run() on class member functions: http://doc.qt.io/qt-5/qtconcurrentrun.html#using-member-functions

        Please remember that functions for GUI-related classes cannot be called from another thread.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1
        • M Offline
          M Offline
          McLion
          wrote on 30 Sept 2015, 09:48 last edited by
          #4

          Thank you guys.
          It is working now and the function perfectly runs in a different thread. Thanks also for the hint with the GUI-related classes. I had some output to the GUI that generated errors at runtime and did not show. Funny it did show once the GUI was hide and show again. So, if I need the output, I will have to implement something different.

          However, I still have an issue with the FutureWatcher. The SLOT that should be called upon finished never gets called.

          QFutureWatcher<char> watcher;
          connect(&watcher, SIGNAL(finished()), this, SLOT(FinishedFormat()));
          QFuture<char> Format = QtConcurrent::run(this, &QTGUI_MainWindow::FormatSDcardForLivePause);
          watcher.setFuture(Format);
          
          void QTGUI_MainWindow::FinishedFormat()
          

          I need to know when it's finished as well as getting the return value (false or true - not implemented yet). Any idea what I am doing wrong that it gets not called at all?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            McLion
            wrote on 30 Sept 2015, 11:56 last edited by
            #5

            Update:
            Found the cause for the SLOT not being called. Moved declaration of QFutureWatcher and QFuture to the constructor of the class:

            FormatWatcher = new QFutureWatcher<char>;
            QFuture<char> Format;
            connect(FormatWatcher, SIGNAL(finished()), this, SLOT(FinishedFormat()));
            

            Now trying to get the result/return value ...

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 30 Sept 2015, 21:46 last edited by
              #6
              FormatWatcher = new QFutureWatcher<char>;
              QFuture<char> Format;
              connect(FormatWatcher, SIGNAL(finished()), this, SLOT(FinishedFormat()));
              watcher->setFuture(Format);
              

              Then you can retrieve the future from the watcher and get the result you need

              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
              0
              • M Offline
                M Offline
                McLion
                wrote on 1 Oct 2015, 07:43 last edited by
                #7

                Everything works as supposed now.
                Calling cSuccess = FormatWatcher->result(); in the FinishedFormat SLOT gets the result.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 1 Oct 2015, 20:38 last edited by
                  #8

                  I've missed that one :D Thanks for reminding me.

                  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
                  0
                  • M Offline
                    M Offline
                    McLion
                    wrote on 2 Dec 2015, 16:35 last edited by
                    #9

                    I'd like to add:

                    Specially if the Watcher needs to be used multiple times it's important to disconnect it after use:

                    disconnect(FormatWatcher, SIGNAL(finished()), this, SLOT(FinishedFormat()));       // disconnect former slot used with watcher
                    
                    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