Results 1 to 4 of 4

Thread: QTimer and QTimeLine

  1. #1
    Join Date
    May 2009
    Posts
    14
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer and QTimeLine

    Hi all,

    please I need your help. What I want to do is, the user introduce in a form a total time of running, and an interval time. In each interval of the total time, several values are sent and several values are received. In a progress bar the total time is showed.

    When someone pressed the stop button or the total time is finished, it calls a function that stop the timeline and the timer and call another function that shows a file dialog to save all the values in a XML.

    The problem is that there is something worng in the timer or in the timeline, because when i run and then press the stop button, the file dialog is shown, but if i cancel the file dialog quick, another file dialog is shown, is like the stopall function is called several times. When the progrees bar end, it don´t happen.

    Here is the code:

    Qt Code:
    1. void Controller_run::execute(){
    2.  
    3. int frequency = run->ui.leFrequency->text().toInt();
    4. int total = run->ui.leTotal->text().toInt();
    5.  
    6. timerFrequency = new QTimer(this);
    7. timeLine = new QTimeLine;
    8.  
    9. timeLine->setDuration(total);
    10. timeLine->setFrameRange(0, 150); // update every second
    11. timeLine->setLoopCount(1);
    12.  
    13. run->ui.progressBar->setRange(0, 150);
    14.  
    15. connect(timerFrequency, SIGNAL(timeout()), this, SLOT(send_receive()));
    16. connect(timeLine, SIGNAL(frameChanged(int)), run->ui.progressBar, SLOT(setValue(int)));
    17. run->ui.progressBar->setValue(0);
    18. connect(run->ui.btStop, SIGNAL (clicked()), this, SLOT(stopall()));
    19. connect(timeLine, SIGNAL(finished()), this, SLOT(stopall()));
    20.  
    21. timerFrequency->start(frequency);
    22. timeLine->start();
    23. }
    24.  
    25. void Controller_run::stopall(){
    26.  
    27. timerFrequency->deleteLater();
    28. timeLine->stop();
    29.  
    30. run->ui.btRun->setEnabled("true");
    31. run->ui.btStop->setDisabled("true");
    32.  
    33. save_xml_file();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Please help me, i am desperated.

    Thanks in advanced.

  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: QTimer and QTimeLine

    Try this:
    Qt Code:
    1. void Controller_run::stopall(){
    2. timerFrequency->stop();
    3. timerFrequency->deleteLater();
    4. timeLine->stop();
    5.  
    6. run->ui.btRun->setEnabled("true");
    7. run->ui.btStop->setDisabled("true");
    8.  
    9. save_xml_file();
    10. }
    To copy to clipboard, switch view to plain text mode 

    P.S
    This is not good practice:
    Qt Code:
    1. timerFrequency = new QTimer(this);
    2. timeLine = new QTimeLine;
    To copy to clipboard, switch view to plain text mode 

    in a slot, since each time this slot is called a new instance for the timer and timeline is allocated, where as the old one is still alive (unless you made sure to delete them before this slot is called).
    It is better to put initialization in the constructor or an init() function which is called once at start.
    ==========================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.

  3. #3
    Join Date
    May 2009
    Posts
    14
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer and QTimeLine

    Hello high_flyer,

    i have tryed what you said, but it doesn´t work, it happens the same, if i press the stop button quickly the function calls save dialog, if i cancel the save dialog, quickly, another save dialog is shown, the stopall is called several times.

    I delete QTimer and QTimeLine, in the stopall function, but as this function is called several times, it occurs an error because is trying to delete QTimer that it doesn´t exists.

    Another idea please!!

  4. #4
    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: QTimer and QTimeLine

    i have tryed what you said, but it doesn´t work, it happens the same, if i press the stop button quickly the function calls save dialog, if i cancel the save dialog, quickly, another save dialog is shown, the stopall is called several times.
    I think I know why - its because your stopall() is called both by the button and by the timer.
    So if you click the button, before the timer times out, the slot will be called twice.

    I delete QTimer and QTimeLine, in the stopall function, but as this function is called several times, it occurs an error because is trying to delete QTimer that it doesn´t exists.
    Right, which is why I wrote in the previous post:
    It is better to put initialization in the constructor or an init() function which is called once at start.
    ==========================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.

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.