Results 1 to 14 of 14

Thread: QFutureWatcher finished() signal not working

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default QFutureWatcher finished() signal not working

    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...

    Qt Code:
    1. void MyClass::doIt()
    2. {
    3. QFuture<bool> future = QtConcurrent::run(
    4. this,
    5. &MyClass::performTask );
    6.  
    7. QFutureWatcher<bool> watcher;
    8. connect( &watcher, SIGNAL( finished() ), this, SLOT( taskCompleted() ) ); // returns true
    9. watcher.setFuture( future );
    10.  
    11. future.begin();
    12. }
    13.  
    14. void MyClass::performTask()
    15. {
    16. //..........
    17. }
    18.  
    19. void MyClass::taskCompleted()
    20. {
    21. // Never gets here
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by DiamonDogX; 10th June 2009 at 21:20. Reason: updated contents

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QFutureWatcher finished() signal not working

    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.
    Last edited by jpn; 10th June 2009 at 22:56. Reason: updated contents
    J-P Nurmi

  3. #3
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    Yes, it is declared as a slot. Yes, MyClass contains the Q_OBJECT macro.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QFutureWatcher finished() signal not working

    Musn't MyClass:: performTask() return bool since you declared QFuture<bool>.

  5. #5
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    It does return bool. I typed it into my post wrong, sorry.

  6. #6
    Join Date
    Jun 2009
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    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):
    Qt Code:
    1. void MyClass::doIt()
    2. {
    3. QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>();
    4. connect( &watcher, SIGNAL( finished() ), this, SLOT( taskCompleted() ) );
    5.  
    6. QFuture<bool> future = QtConcurrent::run( this, &MyClass::performTask );
    7. watcher.setFuture( future );
    8. }
    9.  
    10. bool MyClass::performTask()
    11. {
    12. //..........
    13. }
    14.  
    15. void MyClass::taskCompleted()
    16. {
    17. // Never gets here
    18. }
    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.

  7. The following user says thank you to jangi for this useful post:

    DiamonDogX (16th June 2009)

  8. #7
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    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?

  9. #8
    Join Date
    Jun 2009
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    I'm not sure. Post your new code, including class declaration

  10. #9
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    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.

  11. #10
    Join Date
    Jun 2009
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    Hmm. Any chance that you're using a timer, or somehow calling doIt() a second time before taskCompleted() is called?

  12. #11
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    Hmm nevermind, managed to get it...

  13. #12
    Join Date
    Jun 2009
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    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.

  14. #13
    Join Date
    Oct 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    > Hmm nevermind, managed to get it...

    Any chance you could fill the rest of us in?

  15. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFutureWatcher finished() signal not working

    Quote Originally Posted by dmswart View Post
    > 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

Similar Threads

  1. How to use progressTextChanged() signal in QFutureWatcher
    By DiamonDogX in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 16:41
  2. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 08:16
  3. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 08:04
  4. Signal Slot not working
    By munna in forum Qt Programming
    Replies: 8
    Last Post: 6th November 2006, 11:36
  5. QTreeWidget signal not working
    By Kapil in forum Newbie
    Replies: 6
    Last Post: 28th April 2006, 11:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.