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

9th October 2015, 07: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


if(status == 0)

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




void myapp::ontimeout()


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()



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?

9th October 2015, 12: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.