保存按钮的全局坐标
1 2 3 4
| private: QMap<QString, QRect> m_geometries; QString m_lastButtonName; bool m_isButtonPressed = false;
|
showEvent之后,按钮的坐标才会真正以设置的像素点进行更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| void ButtonWidget::showEvent(QShowEvent* event) { QWidget::showEvent(event); m_geometries = getButtonsGeometries(); }
QMap<QString, QRect> ButtonWidget::getButtonsGeometries() { QMap<QString, QRect> buttonGeometries; buttonGeometries["m_btnWarning"] = ui->m_btnWarning->geometry(); buttonGeometries["m_btnUnlock"] = ui->m_btnUnlock->geometry(); buttonGeometries["m_btnRelease"] = ui->m_btnRelease->geometry(); buttonGeometries["m_btnForwardOne"] = ui->m_btnForwardOne->geometry(); buttonGeometries["m_btnShuntingTwo"] = ui->m_btnShuntingTwo->geometry(); buttonGeometries["m_btnParkingThree"] = ui->m_btnParkingThree->geometry(); buttonGeometries["m_btnRouteNumberFour"] = ui->m_btnRouteNumberFour->geometry(); buttonGeometries["m_btnCalibrationFive"] = ui->m_btnCalibrationFive->geometry(); buttonGeometries["m_btnBackwardSix"] = ui->m_btnBackwardSix->geometry(); buttonGeometries["m_btnStartSeven"] = ui->m_btnStartSeven->geometry(); buttonGeometries["m_btnAutoCorrectEight"] = ui->m_btnAutoCorrectEight->geometry(); buttonGeometries["m_btnModeNine"] = ui->m_btnModeNine->geometry(); buttonGeometries["m_btnInspectionZero"] = ui->m_btnInspectionZero->geometry(); buttonGeometries["m_btnQuery"] = ui->m_btnQuery->geometry(); buttonGeometries["m_btnLeftArrow"] = ui->m_btnLeftArrow->geometry(); buttonGeometries["m_btnUpArrow"] = ui->m_btnUpArrow->geometry(); buttonGeometries["m_btnDownArrow"] = ui->m_btnDownArrow->geometry(); buttonGeometries["m_btnRightArrow"] = ui->m_btnRightArrow->geometry(); buttonGeometries["m_btnDump"] = ui->m_btnDump->geometry(); buttonGeometries["m_btnSetting"] = ui->m_btnSetting->geometry(); buttonGeometries["m_btnConfirm"] = ui->m_btnConfirm->geometry();
return buttonGeometries; }
|
处理像素点
得到一个像素点,遍历容器,寻找是否在容器的范围内
- 如果新点击一个按钮,设置按钮标志位foundButton,检查上一个按钮的名字和这个按钮的名字是否相等,如果不等,触发上一个按钮的函数(因为一直再发送像素点,应该不会出现这个问题);如果不等,就记录按钮名字,设置按钮状态为按下,发出pressed()信号
- 如果点击到其他非按钮区域,检查前一个按钮是否执行了,如果没有执行,触发函数released()信号和clicked()信号,重置资源
- 三种信号互不影响,一般一个按钮使用了clicked()信号,就不会使用pressed()和released()信号,因为clicked()中的函数可以在released()中实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| void ButtonWidget::handleCoordXY(int coordX = 1, int coordY = 1) { if (!m_geometries.isEmpty()) { bool foundButton = false; for (auto it = m_geometries.begin(); it != m_geometries.end(); ++it) { QString buttonName = it.key(); QRect rect = it.value();
if (rect.contains(QPoint(static_cast<int>(coordX), static_cast<int>(coordY)))) { QPushButton* button = findChild<QPushButton*>(buttonName); if (button) { foundButton = true; if (m_lastButtonName != buttonName) { QPushButton* lastButton = findChild<QPushButton*>(m_lastButtonName); if (lastButton && lastButton->isEnabled()) { if (m_isButtonPressed) { emit lastButton->released(); emit button->clicked(); lastButton->setDown(false); m_isButtonPressed = false; } }
m_lastButtonName = buttonName; m_isButtonPressed = true;
emit button->pressed(); button->setDown(true); break; } } } }
if (!foundButton && m_isButtonPressed) { QPushButton* button = findChild<QPushButton*>(m_lastButtonName); if (button) { button->setDown(false); emit button->released(); emit button->clicked();
m_isButtonPressed = false; m_lastButtonName.clear();
} } } }
|