当前位置: 首页 > news >正文

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_())

http://www.dtcms.com/a/469977.html

相关文章:

  • C++ Primer Plus 第六版 第十三章 编程题
  • 大模型前世今生(十二):Hessian矩阵
  • 蛙跳积分法:分子动力学模拟中的高效数值积分技术
  • 详解 SNMPv1 与 SNMPv2 Trap 格式
  • 书法网站建设成都微信公众号制作
  • 宜春网站制作公司wordpress图片上传慢
  • Python串口通信与MQTT物联网网关:连接STM32与物联网平台
  • MyLanViewer(局域网IP扫描软件)
  • 湛江专业建站推荐40平米小户型装修效果图
  • 147.《手写实现 Promise.all 与 Promise.race》
  • 【HarmonyOS】异步并发和多线程并发
  • 使用docker 安装dragonfly带配置文件(x86和arm)版本
  • 企业信息型网站有哪些网站建设塞西
  • 怎么看网站是什么程序做的益阳网络
  • SpringBoot通过配置类替换配置文件配置
  • 使用Customplot绘制时间-数据曲线
  • **量子算法:探索未来的发散创新之路**随着信息技术的飞速发展,量子计算作为
  • 4. 手写数字识别,推理,批处理
  • AI编程时代的文档困境与破局之道:从Cursor到完整开发体系
  • DVWA靶场之十八:API 安全(API Security)
  • ORB_SLAM2原理及代码解析:Optimizer::LocalBundleAdjustment
  • 中文wordpress站点wordpress 获取路径
  • 从零搭建 Kubernetes 1.28 高可用集群
  • 网站建设有什么岗位职责唐山广告设计制作公司
  • Apache Doris 内部数据裁剪与过滤机制的实现原理 | Deep Dive
  • 长沙百度网站建设专精特新中小企业
  • 网站上广告wordpress导出文章word
  • Voron Trident 三叉戟 组装日记
  • 南昌公司做网站网站建设湖南岚鸿建设
  • “零成本自由派”与“钉钉生态派”:斑斑与氚云的选择