PDA

View Full Version : How to use two timers in Qt?



vicky_v
9th October 2015, 08:42
Hello Friends!!
I have one application. in this application two tasks are there. First task repeats every 500 milliseconds and never stops until stop this with separate button and second task repeats 1 seconds and stop after 1 second. for example:

/* in header file myapp.h */
private slots:
QTimer *timer1;
QTimer *timer2;
QSound *sound;
void ontimeout();
void task(); // this task never stops it keep on running continuously


/* in .cpp file myapp.cpp */


/* at constructor */
timer1 = new QTimer();
timer2 = new QTimer();

connect(timer2, SIGNAL(timeout()), this, SLOT(ontimeout()));
connect(timer1, SIGNAL(timeout())),SLOT(task()));

sound = 0;
sound = new("/path/sound.wav"); /* sound file duration is 1 sec. */




/* on private slots */


void myapp::pushbutton()
{
int status;
status = 0

timer1->start();


if(status == 0)
{

ui->label->settext("status true");

}else
{
sound->play()
timer2->start()

}


}




void myapp::ontimeout()
{

if(sound->isFinished())
{

timer2->stop();
ui->label->settext("status failed");

}

}

void myapp::task()
{

/* some task */
/* some task do repeatedly in 500 ms interval and timer never stops */

}


void myapp::pushbutton2()
{

timer1->stop();



}




so the requirement is i want to do sometask after finishing the sound file.

i am using second timer only for playing 1 sec sound file completely becuase i want to playe full duration 1 sec file and then do some task




but problem is that second timer starts when first timer stop.

is anyone have idea about it?

anda_skoa
9th October 2015, 13:43
I am not sure I understand what you problem is.

If timer1 runs continuously, just start it once.
If timer2 should only run once until triggered again, set its singleShot value to true.

Cheers,
_