QFutureWatcher不调用连接插槽 [英] QFutureWatcher not calling connected slot
问题描述
我有以下代码实现 QtConcurrent :: run()
与 QFutureWatcher
启动 fetch()
函数运行一个shell进程。完成后,我想调用 writeDesc
函数,但它从不调用。
I have the following code which implements QtConcurrent::run()
with QFutureWatcher
to start the fetch()
function which runs a shell process. Upon completion, I want to call the writeDesc
function, but it never gets called.
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 ()
工作正常,但程序只显示调试消息线程完成
,而不是 Slot调用
。为什么 writeDesc
被调用?
fetch()
works fine but the program shows only the debug message Thread done
and not Slot called
. Why isn't writeDesc
being called?
推荐答案
问题是,当离开 MyClass :: on_fetchButton_clicked()
方法时,watcher对象被销毁。被销毁的对象不能发出东西。尝试在类的构造函数中分配和连接观察程序。不要忘记在析构器中销毁。
The problem is that the watcher object is destroyed when leaving the MyClass::on_fetchButton_clicked()
method. A destroyed object can not emit something. Try to allocate and connect the watcher in the constructor of your class. Don't forget to destroy in the destructor.
这篇关于QFutureWatcher不调用连接插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!