How to use two timers in Qt?



  • 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:ushbutton()
    {
    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?


  • Moderators

    One comment:

    void myapp:ushbutton()
    {
    int status;
    status = 0
    
    timer1->start();
    
    if(status == 0)
    ...
    

    status will always be 0!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.