SpinBox.cpp 1.2 KB

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