Results 1 to 2 of 2

Thread: Wait for the next action

  1. #1
    Join Date
    Apr 2017
    Posts
    30
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Wait for the next action

    Hi all!

    Tell me, please, how can I make a delay before the next action. In this case, I still need to update the progress bar.
    Qt Code:
    1. void ClassName::start()
    2. {
    3. action1();
    4.  
    5. /*
    6.  
    7.   Here I need to wait a while, update the progress bar, and go further
    8.  
    9.   */
    10.  
    11. action2();
    12. emit finished();
    13. }
    To copy to clipboard, switch view to plain text mode 

    I tried to do it this way, but it does not work.

    Qt Code:
    1. void ClassName::start()
    2. {
    3. emit mainLabelEmit(QString("кек"));
    4. QTimer *timer = new QTimer(this);
    5. timer->setSingleShot(true);
    6. timerCount = 10;
    7. //timer = new QTimer();
    8. timer->start(timerCount * 1000);
    9. while (timer->remainingTime() >= 0)
    10. {
    11. QString smallLabel = QString("Left %1 sec").arg(timer->remainingTime() / 1000);
    12. //qDebug() << smallLabel;
    13. emit smallLabelEmit(smallLabel);
    14. if (timer->remainingTime() == 0)
    15. {
    16. timer->stop();
    17. emit smallLabelEmit(QString(""));
    18. }
    19. else
    20. {
    21. emit progressBarEmit(100 - 100 * timer->remainingTime() / timerCount);
    22. }
    23. }
    24.  
    25. for (int i = 0; i < 7; i++)
    26. {
    27. emit open(i);
    28. }
    29. for (int i = 0; i < 7; i++)
    30. {
    31. emit close(i);
    32. }
    33. emit finished();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Wait for the next action

    I tried to do it this way, but it does not work.
    What does "not work" mean?
    In what way this code is not doing what you expect?

    But I think I can guess:
    You have to remember that QTimer is working in an event loop.
    Your while loop does not return to the main event loop thus your timer never gets started nor can it "tick".
    One way to fix your code is to add processEvents() in to the while loop (and probably once just before it)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 7
    Last Post: 20th September 2014, 12:03
  2. Wait Condition Indication to User Using Loading/Wait Message
    By vivek.panchal in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2012, 16:40
  3. wait until a variable is set
    By kevindebb in forum Qt Programming
    Replies: 4
    Last Post: 7th May 2012, 06:03
  4. how to set wait condition
    By hema in forum Newbie
    Replies: 5
    Last Post: 25th July 2011, 08:08
  5. No action checked in an exclusive action group
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 06: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.