Results 1 to 3 of 3

Thread: QtConcurrent pause

  1. #1
    Join Date
    Apr 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QtConcurrent pause

    Hi,

    I'm having problems with QtConcurrent, how to pause execution of QtConcurrent function (thread) ?
    I've tried QFuture setPaused(true) and pause() but that does not work. Let say I have code like this:

    Qt Code:
    1. void MainWindow::on_pushButton_clicked() // run button
    2. {
    3. future = QtConcurrent::run(this, &MainWindow::my_func);
    4. watcher.setFuture(future);
    5. }
    6.  
    7. void MainWindow::on_pushButton_2_clicked() //pause button
    8. {
    9. future.pause();
    10. return;
    11. }
    12.  
    13. void MainWindow::my_func()
    14. {
    15. qDebug()<< "time-consuming code start";
    16. for (int i = 0; i < 8000; i++)
    17. {
    18. for (int j = 1; j< 1000; j++)
    19. {
    20. sum = sum * i / j;
    21. QApplication::processEvents();
    22.  
    23. }
    24. }
    25. qDebug()<< "time-consuming code end";
    26. }
    To copy to clipboard, switch view to plain text mode 

    When I press pause button my_func() still continues, thread is not paused. what am I doing wrong, how to pause execution of my_func ?
    Thanks in advance.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QtConcurrent pause

    You cannot stop the thread.

    Qt Doc Say's
    Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtConcurrent pause

    And better not use a member function in a UI class, you could be tempted to do UI stuff in it and that would not be OK with a secondary thread.
    Also remove the QApplication:rocessEvent(), the worker thread should not do that either.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    ArkKup (24th April 2013)

Similar Threads

  1. how to pause or resume a download??
    By Ali Reza in forum Newbie
    Replies: 3
    Last Post: 11th June 2012, 20:11
  2. QThread - how to pause and resume
    By xrep in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2012, 08:59
  3. I need a pause (QTimer)
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2008, 05:37
  4. Pause executing QProcess
    By bravetanveer in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2006, 07:55

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.