Search code examples
c++qtqtconcurrent

QFutureWatcher signal not working


My function, finishedCopy(), isn't being called when the QtConcurrent::run function copyFolder is finished. The copyFolder function does complete w/o errors.

 QFutureWatcher<void> watcher;
 connect(&watcher, SIGNAL(finished()), this, SLOT(MainWindow::finishedCopy()));
 QFuture <void> future = QtConcurrent::run(this,&MainWindow::copyFolder,filepath,dir);
 watcher.setFuture(future);


 void MainWindow::finishedCopy()
  {
    QMessageBox::information(this,"","Done");
  }

Solution

  • You need your watcher to live longer.. you are declaring your watcher in the stack, and so it it will get destroyed before the connection signal is emitted.

    Try to declare QFutureWatcher watcher as a member variable in your MainWindow header and then connect the single to the slot in MainWindow constructor