#include "DoubleSpinBox.h" #include #include #include DoubleSpinBox::DoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent) { setButtonSymbols(QAbstractSpinBox::NoButtons); installEventFilter(this); setMaximum(1000000000); setMinimum(-100000000); m_lineEdit = this->lineEdit(); connect(m_lineEdit, &QLineEdit::textChanged, this, [=]() { m_valueChangeFalg = true; }); // 连接 editingFinished 信号来处理编辑完成事件 connect(this, &QDoubleSpinBox::editingFinished, this, [=]() { if (m_valueChangeFalg) { emit editDone(); //if (this->value() != m_oldVal) { setEditColor(); } m_valueChangeFalg = false; // 重置标志 } }); } DoubleSpinBox::~DoubleSpinBox() { } void DoubleSpinBox::setEditColor() { if(this->value()!=m_oldVal) setStyleSheet("color: red;"); //setStyleSheet("background-color: red"); else setStyleSheet(""); } void DoubleSpinBox::setSavedColor() { setStyleSheet(""); m_oldVal = this->value(); } void DoubleSpinBox::setValue(double val) { if(m_oldVal == -1 )m_oldVal = val;//启动软件时将oldVal记录下来,第一次setValue才记录,加个变量 QDoubleSpinBox::setValue(val); } bool DoubleSpinBox::eventFilter(QObject *watched, QEvent *event) { if (event->type() == QEvent::Wheel) { return true; } else { return QDoubleSpinBox::eventFilter(watched, event); } }