Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Stop QTimer and Variable scope issue

Stop QTimer and Variable scope issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 664 Views 2 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.
  • H Offline
    H Offline
    Hiloshi
    wrote on last edited by
    #1

    Hi all,

    I want to have a QTimer keep running, but sometimes I need to stop and resume this timer.
    I have code as below:

    void MainWindow::on_pushButton_3_clicked()
    {
        QTimer *MainLoopTimer;
    
        MainLoopTimer = new QTimer();
        connect(MainLoopTimer, SIGNAL(timeout()), this, SLOT(timerUpDate()));
        MainLoopTimer->start(100);
    }
    
    void MainWindow::on_pushButton_7_pressed()
    {
         MainLoopTimer->stop();
    }
    

    As you can see, "MainLoopTimer" cannot been seen in "on_pushButton_7_pressed()", so I don't know how to stop this timer.

    Appreciate for any suggestion,
    Thanks,

    FlotisableF 1 Reply Last reply
    0
    • H Hiloshi

      Hi all,

      I want to have a QTimer keep running, but sometimes I need to stop and resume this timer.
      I have code as below:

      void MainWindow::on_pushButton_3_clicked()
      {
          QTimer *MainLoopTimer;
      
          MainLoopTimer = new QTimer();
          connect(MainLoopTimer, SIGNAL(timeout()), this, SLOT(timerUpDate()));
          MainLoopTimer->start(100);
      }
      
      void MainWindow::on_pushButton_7_pressed()
      {
           MainLoopTimer->stop();
      }
      

      As you can see, "MainLoopTimer" cannot been seen in "on_pushButton_7_pressed()", so I don't know how to stop this timer.

      Appreciate for any suggestion,
      Thanks,

      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @Hiloshi
      I think you can declare MainLoopTimer as MainWindow's member

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mostefa
        wrote on last edited by
        #3

        Hi @Hiloshi ,

        As suggested by @Flotisable

        Your QTimer can a var member

        in your MainWindow.h

        .
        .
        .
        Private :
        QTimer *mTimer;
        

        In your MainWindow.cpp

        mTimer = new QTimer();
        .
        .
        .
        .
        
        void MainWindow::on_pushButton_7_pressed()
        {
             mTimer->stop();
        }
        .
        .
        .
        
        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 Extensions
        • Unsolved