pyqt 触摸屏监听
都没测试:
pyqt 触摸屏监听
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTouchEventclass TouchWidget(QWidget):def __init__(self):super().__init__()self.setWindowTitle("触摸屏测试")self.label = QLabel("等待触摸...", self)self.label.setGeometry(50, 50, 200, 40)self.resize(300, 200)# 启用触摸事件self.setAttribute(Qt.WA_AcceptTouchEvents, True)def event(self, e):if isinstance(e, QTouchEvent):points = e.touchPoints()if points:p = points[0].pos()self.label.setText(f"触摸: ({p.x():.1f}, {p.y():.1f})")return True # 阻止继续传递给 mouseEventreturn super().event(e)def mousePressEvent(self, e):self.label.setText(f"鼠标点击: ({e.x()}, {e.y()})")if __name__ == "__main__":app = QApplication(sys.argv)w = TouchWidget()w.show()sys.exit(app.exec_())
pyqt 麒麟 系统触摸屏判断 触摸
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt, QTouchEvent, QEvent
from PyQt5.QtGui import QFontclass TouchWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("麒麟系统触摸屏检测")self.setGeometry(100, 100, 800, 600)# 启用触摸事件self.setAttribute(Qt.WA_AcceptTouchEvents, True)# 创建中央部件central_widget = QWidget()self.setCentralWidget(central_widget)# 创建布局和标签layout = QVBoxLayout()central_widget.setLayout(layout)self.info_label = QLabel("触摸屏幕区域进行测试")self.info_label.setAlignment(Qt.AlignCenter)self.info_label.setFont(QFont("Arial", 16))self.info_label.setStyleSheet("background-color: #f0f0f0; padding: 20px;")self.info_label.setMinimumHeight(200)self.touch_info_label = QLabel("触摸信息将显示在这里")self.touch_info_label.setAlignment(Qt.AlignCenter)self.touch_info_label.setStyleSheet("background-color: #e0e0e0; padding: 15px;")layout.addWidget(self.info_label)layout.addWidget(self.touch_info_label)# 触摸点跟踪self.touch_points = {}print("触摸屏检测已启动...")def event(self, event):"""重写event方法处理所有事件"""if event.type() == QEvent.TouchBegin:return self.touchEvent(event)elif event.type() == QEvent.TouchUpdate:return self.touchEvent(event)elif event.type() == QEvent.TouchEnd:return self.touchEvent(event)elif event.type() == QEvent.TouchCancel:return self.touchEvent(event)return super().event(event)def touchEvent(self, event: QTouchEvent):"""处理触摸事件"""event.accept()touch_points = event.touchPoints()current_time = event.timestamp()info_text = f"触摸事件时间: {current_time}ms\n"info_text += f"触摸点数量: {len(touch_points)}\n\n"for i, touch_point in enumerate(touch_points):pos = touch_point.pos()screen_pos = touch_point.screenPos()info_text += f"触摸点 {i+1}:\n"info_text += f" 位置: ({pos.x():.1f}, {pos.y():.1f})\n"info_text += f" 屏幕位置: ({screen_pos.x():.1f}, {screen_pos.y():.1f})\n"info_text += f" 压力: {touch_point.pressure():.2f}\n"info_text += f" 状态: {self.get_touch_state(touch_point.state())}\n"info_text += f" 设备: {touch_point.device().name() if touch_point.device() else '未知'}\n"info_text += f" 唯一ID: {touch_point.uniqueId().constData() if touch_point.uniqueId().isValid() else '无'}\n"info_text += " " + "-"*30 + "\n"# 跟踪触摸点touch_id = touch_point.id()if touch_point.state() == Qt.TouchPointReleased:if touch_id in self.touch_points:del self.touch_points[touch_id]else:self.touch_points[touch_id] = {'position': pos,'state': touch_point.state(),'pressure': touch_point.pressure()}self.touch_info_label.setText(info_text)# 更新主标签显示state_text = "触摸状态: "if event.type() == QEvent.TouchBegin:state_text += "开始触摸"elif event.type() == QEvent.TouchUpdate:state_text += "触摸移动"elif event.type() == QEvent.TouchEnd:state_text += "触摸结束"elif event.type() == QEvent.TouchCancel:state_text += "触摸取消"self.info_label.setText(f"{state_text}\n当前触摸点: {len(self.touch_points)}")return Truedef get_touch_state(self, state):"""获取触摸状态文本"""states = []if state & Qt.TouchPointPressed:states.append("按下")if state & Qt.TouchPointMoved:states.append("移动")if state & Qt.TouchPointStationary:states.append("静止")if state & Qt.TouchPointReleased:states.append("释放")return " | ".join(states) if states else "未知"if __name__ == "__main__":app = QApplication(sys.argv)# 检查触摸设备devices = QTouchDevice.devices()if devices:print(f"检测到 {len(devices)} 个触摸设备:")for device in devices:print(f" 设备名称: {device.name()}")print(f" 设备类型: {device.type().name}")print(f" 最大触摸点: {device.maximumTouchPoints()}")print(f" 能力: {self.get_capabilities_text(device.capabilities())}")print(" " + "="*40)else:print("未检测到触摸设备")window = TouchWindow()window.show()sys.exit(app.exec_())