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