Results 1 to 6 of 6

Thread: Timer doesn't stop

  1. #1
    Join Date
    Feb 2011
    Posts
    16
    Qt products
    Qt5
    Platforms
    Windows

    Default Timer doesn't stop

    Hi,

    I have the problem, that the timer doesn't stop. What I'm doing wrong?

    Qt Code:
    1. QBasicTimer *timer = new QBasicTimer();
    2. timer->start(1000, this);
    3. qDebug() << timer->timerId();
    4. while(timer->isActive()) {
    5. // do something
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Timer doesn't stop

    From QT Doc: This is a fast, lightweight, and low-level class used by Qt internally. We recommend using the higher-level QTimer class rather than this class if you want to use timers in your applications. Note that this timer is a repeating timer that will send subsequent timer events unless the stop() function is called.

  3. #3
    Join Date
    Feb 2011
    Posts
    16
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timer doesn't stop

    Also tried QTimer with setSingleShot(true), same Problem...

  4. #4
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Timer doesn't stop

    Ok. I known problem. You should use qApp->processEvents(); in while loop. I write simple code with two type of timer. Try it.

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QBasicTimer>
    6. #include <QTimer>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private:
    21. void timerEvent(QTimerEvent * event);
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25. void on_pushButton_2_clicked();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29. int isRun;
    30. QBasicTimer *basictimer;
    31. QTimer *timer;
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. isRun = 0;
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. timer = new QTimer();
    20. timer->setSingleShot(true);
    21. timer->start(5000);
    22. int i=0;
    23. while(timer->isActive())
    24. {
    25. ++i;
    26. ui->label->setText("Running " + QString::number(i));
    27. qApp->processEvents();
    28. }
    29. ui->label->setText("Stopped");
    30. delete timer;
    31. }
    32.  
    33. void MainWindow::on_pushButton_2_clicked()
    34. {
    35. basictimer = new QBasicTimer();
    36. isRun=1;
    37. basictimer->start(5000, this);
    38. int i=0;
    39. while(isRun)
    40. {
    41. ++i;
    42. ui->label->setText("Running " + QString::number(i));
    43. qApp->processEvents();
    44. }
    45. ui->label->setText("Stopped");
    46. delete basictimer;
    47. }
    48.  
    49. void MainWindow::timerEvent(QTimerEvent * event)
    50. {
    51. if(event->timerId()==basictimer->timerId())
    52. {
    53. basictimer->stop();
    54. isRun=0;
    55. }
    56. else QMainWindow::timerEvent(event);
    57. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to unit for this useful post:

    vinithr (11th June 2012)

  6. #5
    Join Date
    Feb 2011
    Posts
    16
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timer doesn't stop

    Thanks, it's working now. Used your first example with QTimer.

  7. #6
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Timer doesn't stop

    Thanks This post really helped me lot.

Similar Threads

  1. Replies: 4
    Last Post: 3rd November 2011, 08:30
  2. timer problem(timer does not work)
    By masuk in forum Newbie
    Replies: 6
    Last Post: 14th February 2011, 05:00
  3. Qt Creator doesn't stop at breakpoints
    By TheSaw in forum Qt Tools
    Replies: 3
    Last Post: 12th May 2009, 15:53
  4. implemet a timer
    By adamatic in forum Qt Programming
    Replies: 12
    Last Post: 17th February 2009, 07:31
  5. Program freeze when timer->stop();
    By gQt in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2009, 14:05

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.