定时器函数扭曲可拖动的线段

Timer function distorting a draggable line segment

我有一个可拖动线段。我还有一个计时器。当我在 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) ;

我注意到当我启用计时器时这条线段会变形(见图)(基本上有一个数字时钟可以在小部件的左侧查看 运行 时间)。

我为计时器设置了三个函数:

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

    }

}

如果我关闭定时器,这个问题就不存在了。你能在这里发现问题吗?

会不会是你的更新方式和QWidget::update冲突了? (参见 https://doc.qt.io/qt-5/qwidget.html#update

我不完全确定 class 你的代码是什么 运行 它(什么是基础 class),但对我来说这似乎是一个合理的问题。尝试将您的更新方法重命名为 onTimeout 或类似名称。

此外,尝试使用 &YourClass::yourSlot 语法代替 SLOT(yourSlot()),因为后者即将消失。