【问题标题】:QFutureWatcher not calling connected slotQFutureWatcher 没有调用连接的插槽
【发布时间】:2014-03-22 13:53:41
【问题描述】:

我有以下代码实现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

【问题讨论】:

  • 提醒:由于自动变量在函数结束时被销毁而导致的问题的问题将被关闭。它们属于“简单错字”类别。 OTOH,确实 Qt 可以在一些特别容易被滥用的类的析构函数中提供 qWarning。

标签: c++ qt qtconcurrent


【解决方案1】:

问题是观察者对象在离开MyClass::on_fetchButton_clicked()方法时被销毁了。被破坏的物体不能发射东西。尝试在类的构造函数中分配和连接观察者。不要忘记在析构函数中销毁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 2012-05-26
    • 1970-01-01
    相关资源
    最近更新 更多