DoubleSpinBox.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "DoubleSpinBox.h"
  2. #include <QLineEdit>
  3. #include <QEvent>
  4. #include <QDebug>
  5. DoubleSpinBox::DoubleSpinBox(QWidget *parent)
  6. : QDoubleSpinBox(parent)
  7. {
  8. setButtonSymbols(QAbstractSpinBox::NoButtons);
  9. installEventFilter(this);
  10. setMaximum(1000000000);
  11. setMinimum(-100000000);
  12. m_lineEdit = this->lineEdit();
  13. connect(m_lineEdit, &QLineEdit::textChanged, this, [=]() {
  14. m_valueChangeFalg = true;
  15. });
  16. // 连接 editingFinished 信号来处理编辑完成事件
  17. connect(this, &QDoubleSpinBox::editingFinished, this, [=]() {
  18. if (m_valueChangeFalg)
  19. {
  20. emit editDone();
  21. //if (this->value() != m_oldVal)
  22. {
  23. setEditColor();
  24. }
  25. m_valueChangeFalg = false; // 重置标志
  26. }
  27. });
  28. }
  29. DoubleSpinBox::~DoubleSpinBox()
  30. {
  31. }
  32. void DoubleSpinBox::setEditColor()
  33. {
  34. if(this->value()!=m_oldVal)
  35. setStyleSheet("color: red;");
  36. //setStyleSheet("background-color: red");
  37. else setStyleSheet("");
  38. }
  39. void DoubleSpinBox::setSavedColor()
  40. {
  41. setStyleSheet("");
  42. m_oldVal = this->value();
  43. }
  44. void DoubleSpinBox::setValue(double val)
  45. {
  46. if(m_oldVal == -1 )m_oldVal = val;//启动软件时将oldVal记录下来,第一次setValue才记录,加个变量
  47. QDoubleSpinBox::setValue(val);
  48. }
  49. bool DoubleSpinBox::eventFilter(QObject *watched, QEvent *event)
  50. {
  51. if (event->type() == QEvent::Wheel) {
  52. return true;
  53. } else {
  54. return QDoubleSpinBox::eventFilter(watched, event);
  55. }
  56. }