Search code examples
c++qttimerruntime-error

Timer function distorting a draggable line segment


I've a draggable line segment. Also I've got a timer. This line segment is getting distorted when I operate a timer in this window.

protected:
   //  override / make our own of these function to track mouse movement and 
         void mousePressEvent(QMouseEvent *event) ;
         void mouseReleaseEvent(QMouseEvent *event) ;
         void mouseMoveEvent(QMouseEvent *event) ;

I've noticed this line segment is getting distorted (see figure) when I enable a timer (basically there is a digital clock to see running time in the left side of the widget).

enter image description here

I've following three functions for the timer:

public slots:

        void update();
        void startStopTimer();
        void resetTimer();

  //slot
    connect(ui->pushButton_stimStart, &QPushButton::clicked, this, &ProgramKeyGripV2::startStopTimer);
    connect(ui->pushButton_stimStop, &QPushButton::clicked, this, &ProgramKeyGripV2::resetTimer);

    QTimer *timer2 = new QTimer(this);
    connect(timer2, SIGNAL(timeout()), this, SLOT(update()));
    timer2->start(10);


void ProgramKeyGripV2::startStopTimer()
{
    if(watch->isRunning()) {
        //ui->startStopButton->setText("Restart");
        watch->pause();
    }
    else {
        //ui->startStopButton->setText("Pause");
        watch->start();
    }

}

void ProgramKeyGripV2::resetTimer()
{
    ui->hundredthsText->setText("00");
    ui->secondsText->setText("00");
    ui->minutesText->setText("00");
    watch->reset();
}

void ProgramKeyGripV2::update()
{
    QPalette p = ui->secondsText->palette();
    if(watch->isRunning())
    {
        qint64 time = watch->getTime();
        int h = time / 1000 / 60 / 60;
        int m = (time / 1000 / 60) - (h * 60);
        int s = (time / 1000) - (m * 60);
        int ms = time - ( s + ( m + ( h * 60)) * 60) * 1000;
        int ms_dis = ms / 10;
        if(ms_dis < 10) {
            ui->hundredthsText->setText(QStringLiteral("0%1").arg(ms_dis));
        }
        else {
            ui->hundredthsText->setText(QStringLiteral("%1").arg(ms_dis));
        }
        if(s < 10) {
            ui->secondsText->setText(QStringLiteral("0%1").arg(s));
           // p.setColor(QPalette::Base, Qt::white);
            //ui->secondsText->setPalette(p);
        }
        else {
            ui->secondsText->setText(QStringLiteral("%1").arg(s));

        }
        if(m < 10) {
            ui->minutesText->setText(QStringLiteral("0%1").arg(m));
        }
        else {
            ui->minutesText->setText(QStringLiteral("%1").arg(m));
        }

    }

}

This problem is not there if I disable the timer. Can you spot an issue here?


Solution

  • Could it be that your update method clashes with QWidget::update? (see https://doc.qt.io/qt-5/qwidget.html#update)

    I'm not entire sure about what class your code is running it (what is the base class), but it seems like a plausable issue to me. Try renaming your update method to onTimeout or something like that instead.

    Also, try using the &YourClass::yourSlot syntax instead of SLOT(yourSlot()), as the latter is going away.