下面的代码用QFutureWatcher实现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没有被调用?
发布于 2014-03-22 14:11:47
问题是,当离开MyClass::on_fetchButton_clicked()方法时,观察者对象会被破坏。被摧毁的物体不能发射某种东西。尝试在类的构造函数中分配和连接观察者。别忘了在毁灭器里毁灭。
https://stackoverflow.com/questions/22578471
复制相似问题