Results 1 to 2 of 2

Thread: Little Qtimer problem

  1. #1
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Little Qtimer problem

    Hi !

    if a situation meeting certain conditions occurs, I display a warning message (with a QMessageBox). I use a QTimer to remind the user 5 minutes later that the situation is still happening :

    Qt Code:
    1. void data::data()
    2. {
    3. warning1=true;
    4. }
    5.  
    6. void data::event ()
    7. {
    8. Qtimer *timer2 = new QTimer(this);
    9. QObject::connect( timer2, SIGNAL(timeout()),this,SLOT(warning1_timer2()));
    10.  
    11. if (((condition1)||(condition2))&&(warning1) ) //Conditions
    12. {
    13. warning1=false;
    14. int answer = QMessageBox::warning(this, "Problem detected",QMessageBox::Ok);
    15. if (answer == QMessageBox::Ok)
    16. {
    17. timer2->start(300000);
    18.  
    19. }
    20.  
    21. }
    22. }
    23.  
    24. void Data::warning1_timer2()
    25. {
    26.  
    27. warning1=true;
    28. }
    To copy to clipboard, switch view to plain text mode 

    This works when the situation occurs once...If the situation is still occuring five minutes later, the QMessageBox appears once again but freezes..I know this is due to the statement timer2->start();, but I can't find how to solve my problem!!!

    Thanks for your help !!!
    Last edited by gen_mass; 22nd July 2010 at 20:42.

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Little Qtimer problem

    Why do you create a timer2 object inside data::event() function (is it a slot?) ?

    When you call data::event() again, there will be another "timer2" object and can cause memory leak (if your application doesn't freeze).

    I suggest that you declare timer2 as a member of your class, create timer2 object in your constructor, set the interval, then connecting timer2 timeout() signal to your warning_timer2() slot.

    Qt Code:
    1. data::data(...) : ...
    2. {
    3. ....
    4. timer2 = new QTimer(this);
    5. timer2->setInterval(300000);
    6. timer2->setSingleShot(true);
    7. connect(timer2, SIGNAL(timeout()), this, SLOT(warning_timer2()));
    8. ....
    9. }
    To copy to clipboard, switch view to plain text mode 

    Then, in your data::event()

    Qt Code:
    1. void data::event ()
    2. {
    3. // Qtimer *timer2 = new QTimer(this);
    4. // QObject::connect( timer2, SIGNAL(timeout()),this,SLOT(warning1_timer2()));
    5.  
    6. if (((condition1)||(condition2))&&(warning1) ) //Conditions
    7. {
    8. warning1=false;
    9. int answer = QMessageBox::warning(this, "Problem detected",QMessageBox::Ok);
    10. if (answer == QMessageBox::Ok)
    11. {
    12. // timer2->start(300000);
    13. timer2->start();
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. problem in qtimer
    By mohanakrishnan in forum Qt Programming
    Replies: 4
    Last Post: 26th November 2009, 11:55
  2. Problem with QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2009, 21:51
  3. problem with QTimer
    By salmanmanekia in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2008, 13:22
  4. Problem about QTimer
    By pawer in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2008, 13:02
  5. QTimer problem
    By vv in forum Qt Programming
    Replies: 9
    Last Post: 3rd July 2007, 08:13

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.