[Solved] Running function with QtConcurrent::run



  • 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


  • Lifetime Qt Champion

    Hi,

    You function belongs to QTGUI_MainWindow, see QtConcurrentRun using member functions


  • Moderators

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



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



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


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

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



  • 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
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.