PDA

View Full Version : Little Qtimer problem



gen_mass
22nd July 2010, 19:09
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 :



void data::data()
{
warning1=true;
}

void data::event ()
{
Qtimer *timer2 = new QTimer(this);
QObject::connect( timer2, SIGNAL(timeout()),this,SLOT(warning1_timer2()));

if (((condition1)||(condition2))&&(warning1) ) //Conditions
{
warning1=false;
int answer = QMessageBox::warning(this, "Problem detected",QMessageBox::Ok);
if (answer == QMessageBox::Ok)
{
timer2->start(300000);

}

}
}

void Data::warning1_timer2()
{

warning1=true;
}



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 !!!

saa7_go
24th July 2010, 03:19
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.



data::data(...) : ...
{
....
timer2 = new QTimer(this);
timer2->setInterval(300000);
timer2->setSingleShot(true);
connect(timer2, SIGNAL(timeout()), this, SLOT(warning_timer2()));
....
}

Then, in your data::event()



void data::event ()
{
// Qtimer *timer2 = new QTimer(this);
// QObject::connect( timer2, SIGNAL(timeout()),this,SLOT(warning1_timer2()));

if (((condition1)||(condition2))&&(warning1) ) //Conditions
{
warning1=false;
int answer = QMessageBox::warning(this, "Problem detected",QMessageBox::Ok);
if (answer == QMessageBox::Ok)
{
// timer2->start(300000);
timer2->start();
}
}
}