Results 1 to 5 of 5

Thread: Multiple QTimer issue

  1. #1
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple QTimer issue

    I've encounter a problem for having 2 timers.
    I will use simple code below to explain on it.

    Qt Code:
    1. static int a = 0;
    2.  
    3. MainWindow::MainWindow(QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. connect(&timer_1,SIGNAL(timeout()),this,SLOT(on_timer_1_timeout()));
    9. connect(&timer_2,SIGNAL(timeout()),this,SLOT(on_timer_2_timeout()));
    10. timer_1.start(100);
    11. timer_2.start(1000);
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_timer_1_timeout()
    20. {
    21. a++;
    22. bool b_out=true;
    23. while(b_out)
    24. {
    25. QCoreApplication::processEvents(); //a breakpoint here
    26. Sleep(500);
    27. }
    28. }
    29.  
    30. void MainWindow::on_timer_2_timeout()
    31. {
    32. a++;
    33. bool b_out=true;
    34. while(b_out)
    35. {
    36. QCoreApplication::processEvents(); //a breakpoint here
    37. Sleep(500);
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    Once i start the program, the program is stop at the breakpoint inside on_timer_1_timeout(), the variable a is 1.
    And then i continue the program, the program will stop at the breakpoint inside on_timer_2_timeout(), the variable a is 2.
    After that, i keep on continue the program, the program will keep on stuck at on_timer_2_timeout() and the variable a is 2.
    What should I do so that the program will back to on_timer_1_timeout() and remain the variable a is 2?
    I've try on stating the connection is Qt::QueuedConnection, it is able to go to on_timer_1_timeout() but the variable a will keep on increasing and this is not what I want.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple QTimer issue

    Can you explain what you are trying to achieve because what you are doing is nonsensical.

  3. #3
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple QTimer issue

    Quote Originally Posted by ChrisW67 View Post
    Can you explain what you are trying to achieve because what you are doing is nonsensical.
    Ok. I will explain more on it. I have 2 QTimers.
    When timer1 is timeout, on_timer_1_timeout() will execute some operation which may take some time.
    During the operation, timer2 may timeout, on_timer_1_timeout() will stop at the middle and on_timer_2_timeout() will execute.
    I want on_timer_2_timeout() wait until on_timer_1_timeout() execute finish. (I will use a flag variable to indicate the operation finished)
    I've tried to use
    Qt Code:
    1. while(flag)
    2. {
    3. QCoreApplication::processEvents();
    4. Sleep(500);
    5. }
    To copy to clipboard, switch view to plain text mode 
    to let the program resume the on_timer_1_timeout() but it seems failed.
    The program will stuck in on_timer_2_timeout().
    Based on my research, it seems like need to do some multi-threading on it.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple QTimer issue

    QTimer cannot suspend a running process at an arbitrary point, service another interrupt, and then resume where it left off: this is low-level and real time operating system stuff typically.

    It really does not make sense to attach a process that never returns or takes longer than the timer period (you do both) to a timer that fires every 100 or 1000 milliseconds. If the processes are processing queues of repetitive short processing steps then a simple redesign of what is done in response to a timeout may suffice (you might then use QtConcurrent or feed a queue to a worker thread).

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple QTimer issue

    Adding to what Chris said, if you remove calls to processEvents() from your code, the program will sort-of do what you want. timer2 will not timeout before timer1 does its job. However it still doesn't make sense with timer timeouts set so short.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. Replies: 11
    Last Post: 24th July 2012, 12:39
  4. multiple QTimer::singleShot() calls?
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2009, 19:22
  5. QTimer issue
    By qball2k5 in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2006, 18:14

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.