pyqt 触摸屏监听

都没测试:

pyqt 触摸屏监听

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTouchEvent

class 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  # 阻止继续传递给 mouseEvent
        return 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 麒麟 系统触摸屏判断 触摸

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt, QTouchEvent, QEvent
from PyQt5.QtGui import QFont

class 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 True

    def 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_())
相关推荐
景彡先生4 小时前
Python Selenium详解:从入门到实战,Web自动化的“瑞士军刀”
前端·python·selenium
程序员卷卷狗4 小时前
JVM 调优实战:从线上问题复盘到精细化内存治理
java·开发语言·jvm
lly2024065 小时前
ASP Folder:深入解析其功能与使用技巧
开发语言
雪域迷影5 小时前
Go语言中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·后端·http·golang·get
珺毅同学6 小时前
YOLO输出COCO指标及YOLOv12报错
python·深度学习·yolo
ysdysyn7 小时前
C# 进程管理实战:检查与启动EXE程序的完整指南
开发语言·c#
IDOlaoluo7 小时前
PHP-5.2.1.tar.gz 离线安装教程:从源码编译到配置的详细步骤(附安装包)
开发语言·php
2401_841495648 小时前
Windows 系统中ffmpeg安装问题的彻底解决
windows·python·ffmpeg·bug·语音识别·下载·安装步骤
wangjialelele8 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
waysolong909 小时前
MCP服务构建、使用
python