SpeedAdjPage.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "SpeedAdjPage.h"
  2. #include "ui_SpeedAdjPage.h"
  3. #include <QMouseEvent>
  4. #include <QRegularExpression>
  5. #include <QPropertyAnimation>
  6. #include <QScreen>
  7. SpeedAdjPage::SpeedAdjPage(QWidget *parent)
  8. : QMainWindow(parent)
  9. , ui(new Ui::SpeedAdjPage)
  10. {
  11. ui->setupUi(this);
  12. Init();
  13. }
  14. SpeedAdjPage::~SpeedAdjPage()
  15. {
  16. qApp->removeEventFilter(this);
  17. delete ui;
  18. }
  19. void SpeedAdjPage::updateDefVal(const ST_DEF_VAL& val)
  20. {
  21. QString strTip = tr("Mode:", "模组:");
  22. strTip += val.strMode;
  23. strTip += " ";
  24. strTip += tr("Axis:(","轴:(");
  25. strTip += val.strAxis;
  26. /* for (auto&a: val.vecPos)
  27. {
  28. strTip += a.AxisType.c_str();
  29. strTip += ":";
  30. QString s = QString::number(a.pos, 'f', 2);
  31. }*/
  32. strTip += ")";
  33. ui->speedLabel->setText(strTip);
  34. }
  35. void SpeedAdjPage::Init()
  36. {
  37. qApp->installEventFilter(this);
  38. setWindowFlag(Qt::FramelessWindowHint);
  39. //setFocusPolicy(Qt::StrongFocus);
  40. // 绑定按钮
  41. QList<QPushButton*> listPBut;
  42. listPBut.push_back(ui->subBut_1);
  43. listPBut.push_back(ui->addBut_1);
  44. listPBut.push_back(ui->subBut_10);
  45. listPBut.push_back(ui->addBut_10);
  46. listPBut.push_back(ui->subBut_100);
  47. listPBut.push_back(ui->addBut_100);
  48. listPBut.push_back(ui->subBut_1000);
  49. listPBut.push_back(ui->addBut_1000);
  50. listPBut.push_back(ui->subBut_10000);
  51. listPBut.push_back(ui->addBut_10000);
  52. listPBut.push_back(ui->subBut_100000);
  53. listPBut.push_back(ui->addBut_100000);
  54. // 连接每个按钮的clicked信号
  55. for (QPushButton* pBut : listPBut)
  56. {
  57. connect(pBut, &QPushButton::clicked, [this, pBut]()
  58. {
  59. QRegularExpression re("^(sub|add)But_(\\d+)$");
  60. QRegularExpressionMatch match = re.match(pBut->objectName());
  61. if (match.hasMatch())
  62. {
  63. QString prefix = match.captured(1);
  64. int num = match.captured(2).toInt();
  65. int value = (prefix == "sub") ? -num : num;
  66. m_nVal = value; // 更新成员变量
  67. emit CloseWnd(m_nVal);
  68. }
  69. });
  70. }
  71. }
  72. void SpeedAdjPage::mousePressEvent(QMouseEvent* event)
  73. {
  74. if (event->button() == Qt::LeftButton)
  75. {
  76. m_mousePressPosition = event->globalPos();
  77. m_windowPosition = this->frameGeometry().topLeft();
  78. }
  79. }
  80. void SpeedAdjPage::mouseMoveEvent(QMouseEvent* event)
  81. {
  82. if (event->buttons() & Qt::LeftButton)
  83. {
  84. QPoint delta = event->globalPos() - m_mousePressPosition;
  85. this->move(m_windowPosition + delta);
  86. }
  87. }
  88. bool SpeedAdjPage::eventFilter(QObject* obj, QEvent* event)
  89. {
  90. if (event->type() == QEvent::MouseButtonPress)
  91. {
  92. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
  93. QPoint globalPos = mouseEvent->globalPos();
  94. QRect dialogFrame = this->frameGeometry();
  95. if (this->isVisible() && !dialogFrame.contains(globalPos))
  96. {
  97. this->close();
  98. }
  99. }
  100. return false;
  101. }
  102. void SpeedAdjPage::changeEvent(QEvent* event)
  103. {
  104. if (event->type() == QEvent::WindowActivate)
  105. {
  106. // 窗口被激活
  107. //qDebug() << "Main window is now active";
  108. }
  109. else if (event->type() == QEvent::WindowDeactivate)
  110. {
  111. // 窗口失去激活
  112. //qDebug() << "Main window is no longer active";
  113. }
  114. QMainWindow::changeEvent(event);
  115. }
  116. void SpeedAdjPage::on_closeBut_clicked()
  117. {
  118. this->close();
  119. }