Results 1 to 2 of 2

Thread: How to use two timers in Qt?

  1. #1
    Join Date
    Oct 2015
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default 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:ntimeout()
    {

    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:ushbutton2()
    {

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use two timers in Qt?

    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,
    _

Similar Threads

  1. how to use Multiple timers?
    By qtlinuxnewbie in forum Newbie
    Replies: 8
    Last Post: 21st September 2012, 09:01
  2. Qt and posix timers
    By AlbertoXerez in forum Qt-based Software
    Replies: 1
    Last Post: 17th August 2012, 12:24
  3. QLabel + timers
    By prophet0 in forum Qt Programming
    Replies: 4
    Last Post: 26th March 2012, 16:57
  4. Timers seemingly getting stuck.
    By MarkSimon in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2010, 12:30
  5. How to cleanup timers properly?
    By jpn in forum Qt Programming
    Replies: 7
    Last Post: 6th July 2006, 17:52

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
  •  
Qt is a trademark of The Qt Company.