Results 1 to 7 of 7

Thread: Stoping a Qtimer problem...!

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Stoping a Qtimer problem...!

    This is the code:
    Qt Code:
    1. void MainWindow::on_checkBox_clicked()
    2. {
    3. if ( ui->checkBox->isChecked() ){
    4. QTimer *timer = new QTimer(this);
    5. connect(timer, SIGNAL(timeout()), this, SLOT(write_random_image()));
    6. timer->start(1000);
    7. }
    8. else
    9. timer->stop();
    10. }
    To copy to clipboard, switch view to plain text mode 
    actually, I wanna program , when the checkbox is clicked and checked, the timer must start, and if it is clicked and not checked the timer to stop..
    The inly issue is that I get the error:
    error: ‘timer’ was not declared in this scope (this goes to timer->stop()
    Even if I declare the creation of the timer at the constructor (in a try to use it globally in the program) I cannot call it because of the same error...
    So, how can I fix this?

  2. #2
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Stoping a Qtimer problem...!

    I guess you want to declare your QTimer *timer as a private variable in the header of your MainWindow class, initialize it to a new QTimer(this) and connect() in the constructor, and let the click-slot handle start() and stop().

  3. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Stoping a Qtimer problem...!

    Ammm I didn't got it...
    Step 1: I have to put this at the constructor
    connect(timer, SIGNAL(timeout()), this, SLOT(write_random_image()));
    Step 2: I have to put this in myfile.h ????????? After the inclusions (#include <QMainWindow>)
    QTimer *timer = new QTimer;
    Step 3: Let the click-slot handle the start-stop? How do i do this?


    Sorry but I am a beginner. Very willing to learn

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Stoping a Qtimer problem...!

    and also you might want use a unique connection (5th parameter) that the connection is only established once, even if the check box is checked several times.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Stoping a Qtimer problem...!

    Quote Originally Posted by hakermania View Post
    Sorry but I am a beginner. Very willing to learn
    Qt Code:
    1. class Foo
    2. {
    3. //...
    4. private:
    5. QTimer *m_timer;
    6. };
    7.  
    8. Foo::Foo(/*...*/)
    9. {
    10. m_timer = new QTimer(this);
    11. connect(m_timer, SIGNAL(timeout()), this, SLOT(write_random_image()));
    12. }
    13.  
    14. void Foo::on_checkBox_clicked(bool checked)
    15. {
    16. if ( checked )
    17. m_timer->start(1000);
    18. else
    19. m_timer->stop();
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Stoping a Qtimer problem...!

    Ok.

    In the header file of your class (probably mainwindow.h):
    Qt Code:
    1. private:
    2. QTimer *timer;
    To copy to clipboard, switch view to plain text mode 
    In the constructor MainWindow::MainWindow() of your class:
    Qt Code:
    1. timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(write_random_image()));
    To copy to clipboard, switch view to plain text mode 
    In the on_checkBox_clicked() slot:
    Qt Code:
    1. if (ui->checkBox->isChecked())
    2. timer->start(1000);
    3. else
    4. timer->stop();
    To copy to clipboard, switch view to plain text mode 
    Also remember to #include <QTimer> somewhere (e.g. in the header file).

    Edit: Too slow..

  7. The following user says thank you to hobbyist for this useful post:

    hakermania (24th August 2010)

  8. #7
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Stoping a Qtimer problem...!

    Thnk you It works!

Similar Threads

  1. Problem with QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2009, 21:51
  2. Stoping the Screensaver.
    By Grimlock in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2009, 23:30
  3. QTimer problem
    By rajeshs in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2008, 14:33
  4. Problem about QTimer
    By pawer in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2008, 13:02
  5. QTimer problem
    By vv in forum Qt Programming
    Replies: 9
    Last Post: 3rd July 2007, 08:13

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.