문제

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() works fine but the program shows only the debug message Thread done and not Slot called. Why isn't writeDesc being called?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top