PDA

View Full Version : QFutureWatcher finished() signal not working



DiamonDogX
10th June 2009, 21:02
I have a QFuture set up that successfully calls a method and finishes what it is supposed to do, but my slot which is supposed to get notified when it finishes never gets called...
I tried started() as well, and that doesn't work either...


void MyClass::doIt()
{
QFuture<bool> future = QtConcurrent::run(
this,
&MyClass::performTask );

QFutureWatcher<bool> watcher;
connect( &watcher, SIGNAL( finished() ), this, SLOT( taskCompleted() ) ); // returns true
watcher.setFuture( future );

future.begin();
}

void MyClass::performTask()
{
//..........
}

void MyClass::taskCompleted()
{
// Never gets here
}

jpn
10th June 2009, 22:52
Do you see any warning in the debug output? Is MyClass::taskCompleted() declared as slot? Does MyClass contain the required Q_OBJECT macro?

EDIT: Oops, never mind. You said it returns true.

DiamonDogX
11th June 2009, 14:47
Yes, it is declared as a slot. Yes, MyClass contains the Q_OBJECT macro.

Lykurg
11th June 2009, 15:14
Musn't MyClass:: performTask() return bool since you declared QFuture<bool>.

DiamonDogX
11th June 2009, 16:08
It does return bool. I typed it into my post wrong, sorry.

jangi
15th June 2009, 18:56
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
}


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.

DiamonDogX
15th June 2009, 23:22
The pointer variable worked as far as the connect() call. Though when I tried making it a member variable and deleting it in my finished() function, it crashes... any ideas?

jangi
16th June 2009, 19:40
I'm not sure. Post your new code, including class declaration

DiamonDogX
16th June 2009, 21:00
Only thing I did was declare "QFutureWatcher<bool> *watcher;" in my class declaration (and changed the one line where it was previously declared locally: "watcher = new QFutureWatcher<bool>();"). I call "delete watcher" at the end of my taskCompleted() function.

jangi
16th June 2009, 21:09
Hmm. Any chance that you're using a timer, or somehow calling doIt() a second time before taskCompleted() is called?

DiamonDogX
16th June 2009, 22:45
Hmm nevermind, managed to get it...

jangi
16th June 2009, 22:54
np. btw, I looked into it, and the reason it's okay to do the QFuture on the stack is because QFutureWatch makes a copy.

dmswart
25th October 2011, 17:50
> Hmm nevermind, managed to get it...

Any chance you could fill the rest of us in?

d_stranz
25th October 2011, 19:27
> Hmm nevermind, managed to get it...

Any chance you could fill the rest of us in?

This thread is more than 2 years old. It is the future now :)