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. Timer stop from slot

Timer stop from slot

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 15.5k 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.
  • A Offline
    A Offline
    Anna
    wrote on last edited by
    #1

    Hello to all,

    I've got a problem concerning a timer: I would like to have it started when a button is clicked and stop it, when the button is clicked again.
    But I can't access the timer created in the constructor by a slot or a function...
    How can I do it right?

    @Andon::Andon(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Andon)
    {
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    

    }

    void Andon::on_pushButton_clicked()
    {

    if(ui->pushButton->isChecked())
    {
        ui->pushButton->setText("Stop");
        timer.start(1000);
    }
    else
    {
        ui->pushButton->setText("Start");
        timer.stop();
    }
    

    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      you must put the variable holding the pointer to the time as a member variable into the class:

      @
      class QTimer;

      class Andon : public QWidget
      {
      Q_OBJECT
      public:
      Andon(QWidget *parent);

      // more stuff

      private:
      QTimer *timer;
      };

      // in the implementation:

      Andon::Andon(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Andon)
      {
          ui->setupUi(this);
       
          timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
      }
       
       
      void Andon::on_pushButton_clicked()
      {
           
          if(ui->pushButton->isChecked())
          {
              ui->pushButton->setText("Stop");
              timer->start(1000);
          }
          else
          {
              ui->pushButton->setText("Start");
              timer->stop();
          }
      }
      @

      Also not the change of timer.start to timer->start, as you are operating on a pointer.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DeVeL-2010
        wrote on last edited by
        #3

        Hi Anna,

        make *QTimer timer a member variable of your class Andon. Then you can use it also in Andon::on_pushButton_clicked() like
        @ timer->stop();@

        1 Reply Last reply
        0
        • U Offline
          U Offline
          uranusjr
          wrote on last edited by
          #4

          Shouldn't it be timer->stop()?

          To access timer, you need to declare it as an instance variable, not a local variable in the constructor. The following should work:

          @
          class Andon : public QWidget
          {
          Q_OBJECT

          public:
          Andon(QWidget *parent = 0);

          public slots:
          void on_pushButton_clicked();

          private:
          QTimer *timer;
          };

          Andon::Andon(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Andon)
          {
          ui->setupUi(this);

          timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
          

          }

          void Andon::on_pushButton_clicked()
          {

          if(ui->pushButton->isChecked())
          {
              ui->pushButton->setText("Stop");
              timer->start(1000);
          }
          else
          {
              ui->pushButton->setText("Start");
              timer->stop();
          }
          

          }
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anna
            wrote on last edited by
            #5

            thank you for your kind replies!

            I've implemented it in my andon class and also created an instance in the constructor like uranusjr wrote.
            but now i get the error-message: request for member 'start' in '((Andon*)this)->Andon::timer', which is of non-class type 'QTimer*'

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Did you change timer.start() to timer->start() (and the same with stop too)?

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anna
                wrote on last edited by
                #7

                oh, right! that was it ^^

                thank you guys!

                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