Stopwatch with wrong timing

0

I have a QtSDK timer, but when it starts time goes absurdly faster than normal . Here is the code below:

QTime time;
QTimer timer;

void PlanejamentoWidget::timeUpdate(){
    QTime t = ui.timeEdit->time();
    ui.timeEdit->setTime(t.addMSecs(time.elapsed()));
}

void PlanejamentoWidget::startTimer(){
    if (timer.isActive()){
       ui.btStart->setText("Iniciar");
       timer.stop();
    }
    else{       
        ui.btStart->setText("Parar");
        time.start();
        timer.start(60);
    }
}

void PlanejamentoWidget::restartTimer(){
    time.restart();
    QTime t;
    t.setHMS(0, 0, 0, 0);
    ui.timeEdit->setTime(t);
}

Does anyone have any idea what it might be? Thank you.

    
asked by anonymous 23.02.2015 / 16:46

1 answer

1

Your problem is that time.elapsed() is the amount of time since you called time.start() in milliseconds. So the difference is increasing - the value is never zeroed.

To sort, instead of calling #elapsed() use #restart() :

    void PlanejamentoWidget::timeUpdate(){
        QTime t = ui.timeEdit->time();
        ui.timeEdit->setTime(t.addMSecs(time.restart()));
    }

The #restart() method returns the amount of time passed (in milliseconds) since the last call to #start() or #restart() .

See more at: QTime .

    
07.05.2015 / 07:40