QFutureWatcher信号不工作

QFutureWatcher signal not working

本文关键字:工作 信号 QFutureWatcher      更新时间:2023-10-16

当QtConcurrent::run函数copyFolder完成时,我的函数finishedCopy()没有被调用。copyFolder函数没有错误。

 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");
  }

你需要你的手表更长寿。你在堆栈中声明了你的监视程序,所以它会在连接信号发出之前被销毁。

尝试声明QFutureWatcher watcher作为MainWindow头中的成员变量,然后将单个连接到MainWindow构造函数

中的槽

替换为

connect(&watcher, SIGNAL(finished()), this, SLOT(MainWindow::finishedCopy()));
与:

connect(&watcher, SIGNAL(finished()), this, SLOT(finishedCopy()));

此外,connect返回bool,因此您可以始终检查连接是否成功。