Results 1 to 2 of 2

Thread: Implementation of timer makes the program to continuously slow down

  1. #1
    Join Date
    Jan 2006
    Posts
    12
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Implementation of timer makes the program to continuously slow down

    The main idea is to have time shown on Qtable (is there a better class for it?) on main widget. At first the time is updated correctly every second, but after ~10 seconds the time is updated only every 2, 3, etc seconds. Now I think for some reason something makes the program slow down, and for that the clock is updated more infrequent. There may be some loop that i cant notice that continues to eat up the resources more and more after each update:

    Qt Code:
    1. class test : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. test(QWidget *qwi,QLabel *qlb);
    6.  
    7. public slots:
    8. void upd();
    9.  
    10. private:
    11. QWidget *qw;
    12. QLabel *ql;
    13. };
    14.  
    15. test::test(QWidget *qwi, QLabel *qlb)
    16. {
    17. qw = qwi;
    18. ql = qlb;
    19. }
    20.  
    21. void test::upd()
    22. {
    23. QTimer *timer = new QTimer();
    24. connect(timer,SIGNAL(timeout()),this,SLOT(upd()));
    25. timer->start(1000);
    26. QTime time = QTime::currentTime();
    27. QString str = "<font color='green'>";
    28. str.append(time.toString("hh:mm:ss"));
    29. str.append("</font>");
    30. ql->setText(str);
    31. qw->update();
    32. }
    33.  
    34.  
    35. int main (int argc, char *argv[])
    36. {
    37. QApplication app(argc, argv);
    38.  
    39. QWidget window;
    40.  
    41. window.resize(1024, 768);
    42. QLabel *timeLabel = new QLabel(&window);
    43. timeLabel->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
    44. timeLabel->setFont(QFont("Times", 20, QFont::Bold));
    45. timeLabel->setAlignment(Qt::AlignBottom | Qt::AlignRight);
    46. QString str = "<font color='green'>";
    47. QTime time = QTime::currentTime();
    48. str.append(time.toString("hh:mm:ss"));
    49. str.append("</font>");
    50. timeLabel->setText(str);
    51.  
    52. QPalette palette(QApplication::palette());
    53. window.setParent(0,Qt::FramelessWindowHint);
    54. palette.setColor(QPalette::Background, QColor("Black"));
    55. QPushButton quit("Quit", &window);
    56. quit.setFont(QFont("Times", 18, QFont::Bold));
    57. palette.setColor(QPalette::Button, QColor("Blue"));
    58. window.setPalette(palette);
    59. quit.setPalette(palette);
    60. quit.setGeometry(974, 0, 50, 20);
    61. QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
    62.  
    63.  
    64. window.show();
    65.  
    66. test *t = new test(&window,timeLabel);
    67. t->upd();
    68. return app.exec();
    69. }
    70.  
    71. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Implementation of timer makes the program to continuously slow down

    Quote Originally Posted by Yorma
    Now I think for some reason something makes the program slow down, and for that the clock is updated more infrequent. There may be some loop that i cant notice that continues to eat up the resources more and more after each update:

    Qt Code:
    1. void test::upd()
    2. {
    3. QTimer *timer = new QTimer();
    4. connect(timer,SIGNAL(timeout()),this,SLOT(upd()));
    5. timer->start(1000);
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    Every time you invoke test::upd() you create a new timer, so after first second your will have 2 timers, after second one --- 4, then 8, 16, 32, ..., and after a minute you will have about 10^19 timers.

    Create that timer only once.

Similar Threads

  1. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 05: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.