0%

QT_(十二)-按钮长按短按根据像素点区分

保存按钮的全局坐标

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(); // 显示之后更新的坐标才是对标,否则都是0
}

// 采用键值对的形式,保存按钮的objectName和按钮的坐标信息
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;
}

处理像素点

得到一个像素点,遍历容器,寻找是否在容器的范围内

  1. 如果新点击一个按钮,设置按钮标志位foundButton,检查上一个按钮的名字和这个按钮的名字是否相等,如果不等,触发上一个按钮的函数(因为一直再发送像素点,应该不会出现这个问题);如果不等,就记录按钮名字,设置按钮状态为按下,发出pressed()信号
  2. 如果点击到其他非按钮区域,检查前一个按钮是否执行了,如果没有执行,触发函数released()信号和clicked()信号,重置资源
  3. 三种信号互不影响,一般一个按钮使用了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)
{
// 如果前一个按钮在3秒内移动了,不会触发pressed(),所以需要这里进行触发clicked();
QPushButton* lastButton = findChild<QPushButton*>(m_lastButtonName);
if (lastButton && lastButton->isEnabled())
{
// 前一个按钮按下且未重置的情况,需要发送released()信号,pressed()和released()信号都发送的话,可以触发clicked()
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) // 判断前一个clicked()没有触发,也可能是released()没有释放
{
// 找到前一个操作的按钮
QPushButton* button = findChild<QPushButton*>(m_lastButtonName);
if (button)
{
// 恢复按钮状态
button->setDown(false);
emit button->released(); // 触发 released 信号
emit button->clicked();

m_isButtonPressed = false; // 重置状态
m_lastButtonName.clear(); // 清空上一个按钮名称

}
}
}
}

-------------本文结束感谢您的阅读-------------