QFutureWatcher没有调用已连接的插槽

QFutureWatcher not calling connected slot

本文关键字:连接 插槽 调用 QFutureWatcher      更新时间:2023-10-16

我有以下代码实现QtConcurrent::run()QFutureWatcher启动运行shell进程的fetch()函数。完成后,我想调用writeDesc函数,但它从未被调用。

void MyClass::on_fetchButton_clicked()
{
    QFuture<void> fetcher;
    QFutureWatcher<void> watcher;
    connect(&watcher, SIGNAL(finished()), this, SLOT(writeDesc()));
    fetcher = QtConcurrent::run(this, &MyClass::fetch);
    watcher.setFuture(fetcher);
}
void MyClass::fetch()
{
    [...]
    qDebug()<<"Thread done";
}
void MyClass::writeDesc()
{
    qDebug()<<"Slot called";
    [...]
}

fetch()工作正常,但程序只显示调试消息Thread done而不显示Slot called。为什么不叫writeDesc ?

问题是在离开MyClass::on_fetchButton_clicked()方法时监视对象被销毁。一个被破坏的物体不能发出什么东西。尝试在类的构造函数中分配和连接监视器。别忘了在析构函数中销毁。