Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use two timers in Qt?
Qt 6.12 LTS is out! See what's new in the release blog

How to use two timers in Qt?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vicky_V
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      One comment:

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

      status will always be 0!

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved