Results 1 to 4 of 4

Thread: Call a function every x seconds until button pressed

  1. #1
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Call a function every x seconds until button pressed

    Hey there guys, maybe it´s a simple task but it´s driving me a little bit crazy, especially QTime.
    I´m learning minute by minute but sometimes I just need to ask something as I really need help.

    What I would like to do is:
    1- Press START button and every ´x´ seconds the MAIN FUNCTION is called, exactly like I would do in a for cycle and it must stop when I click STOP.
    2- Press STOP button and I exit the loop;

    Right now I have this:

    Qt Code:
    1. void MainWindow::on_pushButton_START_clicked()
    2. {
    3. ... here is the button that I want to use to START the loop of the main function. Every ´x´ seconds MAIN FUNCTION loops.
    4. }
    5.  
    6. void MainWindow::on_pushButton_STOP_clicked()
    7. {
    8. ... here is the button that I want to use to STOP the loop of the main function...
    9. }
    10.  
    11. void MainWindow::on_pushButton_MAIN_FUNCTION_clicked()
    12. {
    13. ... here is the function I want to loop...
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Call a function every x seconds until button pressed

    QTimer
    For 1. call QTimer::start()
    For 2. call QTimer::stop()
    connect the slot or signal you want to call periodically to QTimer::timeout() signal.

  3. The following user says thank you to stampede for this useful post:

    bubble (11th February 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Call a function every x seconds until button pressed

    Thank you so much! The start function is working!

    But how do I use the timer->stop() inside the STOP function in order to stop the timer that was created inside the START function ?


    Qt Code:
    1. void MainWindow::on_pushButton_START_clicked()
    2. {
    3.  
    4. QTimer *timer = new QTimer(this);
    5.  
    6. connect(timer, SIGNAL(timeout()), this, SLOT(on_pushButton_MAIN_FUNCTION_clicked()));
    7. timer->start(1000);
    8.  
    9. }
    10.  
    11. void MainWindow::on_pushButton_STOP_clicked()
    12. {
    13.  
    14. ...
    15.  
    16. }
    17.  
    18.  
    19. void MainWindow::on_pushButton_MAIN_FUNCTION_clicked()
    20. {
    21. ... here is the function I want to loop...
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Call a function every x seconds until button pressed

    Keep the "timer" as member of MainWindow class.

Similar Threads

  1. do something while a button is pressed
    By saman_artorious in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2013, 15:39
  2. Replies: 4
    Last Post: 2nd August 2012, 07:42
  3. Replies: 6
    Last Post: 4th October 2010, 03:19
  4. How to call a function when i clic a button?
    By trend900 in forum Newbie
    Replies: 2
    Last Post: 22nd January 2009, 16:40
  5. Getting the row for button pressed
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2007, 15:45

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.