1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "DoubleSpinBox.h"
- #include <QLineEdit>
- #include <QEvent>
- #include <QDebug>
- 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);
- }
- }
|