我有一个可拖动线段。我也有一个计时器。当我在此窗口中操作计时器时,此线段变得失真。
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)
我不能完全确定您的代码正在运行哪个类(什么是基类),但是对我来说这似乎是一个合理的问题。尝试将更新方法重命名为onTimeout或类似的名称。
另外,请尝试使用&YourClass::yourSlot语法而不是SLOT(yourSlot()),因为后者会消失。
关于c++ - 计时器功能扭曲可拖动的线段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62159969/