Timer stop from slot



  • 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();
    }
    

    }@



  • 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.



  • 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();@



  • 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();
    }
    

    }
    @



  • 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*'



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



  • oh, right! that was it ^^

    thank you guys!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.