First, you don't need the call to QFuture::begin().
The QFutureWatcher object needs to be created on the heap (otherwise it's deleted at the end of the doIt() function):
void MyClass::doIt()
{
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>();
connect( &watcher, SIGNAL( finished() ), this, SLOT( taskCompleted() ) );
QFuture<bool> future = QtConcurrent::run( this, &MyClass::performTask );
watcher.setFuture( future );
}
bool MyClass::performTask()
{
//..........
}
void MyClass::taskCompleted()
{
// Never gets here
}
void MyClass::doIt()
{
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>();
connect( &watcher, SIGNAL( finished() ), this, SLOT( taskCompleted() ) );
QFuture<bool> future = QtConcurrent::run( this, &MyClass::performTask );
watcher.setFuture( future );
}
bool MyClass::performTask()
{
//..........
}
void MyClass::taskCompleted()
{
// Never gets here
}
To copy to clipboard, switch view to plain text mode
Of course this brings some memory management questions into play... How does the watcher get deleted? You could make it a class variable so it can be deleted in the taskCompleted() function. I'm unclear on how the future object works, since in theory it should disappear at the end of the doIt() function as well... but it does work.
Bookmarks