欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PyQT区分左键双击和左键单击

程序员文章站 2024-02-04 19:40:52
```python from PyQt5.QtCore import QTimer from PyQt5 import QtCore, QtGui, QtWidgets class myWidgets(QtWidgets.QTableWidget): def __init__(self, paren... ......
from pyqt5.qtcore import qtimer
from pyqt5 import qtcore, qtgui, qtwidgets

class mywidgets(qtwidgets.qtablewidget):  

    def __init__(self, parent=none):
        super(mywidgets, self).__init__(parent)
        self.isdoubleclick = false
        self.mouse = ""
    def mousepressevent(self, e): 
        # 左键按下
        if e.buttons() == qtcore.qt.leftbutton:
            qtimer.singleshot(0, lambda: self.judgeclick(e))
        # 右键按下
        elif e.buttons() == qtcore.qt.rightbutton:
            self.mouse = "右"
        # 中键按下
        elif e.buttons() == qtcore.qt.midbutton:
            self.mouse = '中'
        # 左右键同时按下
        elif e.buttons() == qtcore.qt.leftbutton | qtcore.qt.rightbutton:
            self.mouse = '左右'
        # 左中键同时按下
        elif e.buttons() == qtcore.qt.leftbutton | qtcore.qt.midbutton:
            self.mouse = '左中'
        # 右中键同时按下
        elif e.buttons() == qtcore.qt.midbutton | qtcore.qt.rightbutton:
            self.mouse = '右中'
        # 左中右键同时按下
        elif e.buttons() == qtcore.qt.leftbutton | qtcore.qt.midbutton | qtcore.qt.rightbutton:
            self.mouse = '左中右'
    def mousedoubleclickevent(self,e):
        # 双击
        self.mouse = "双击"
        self.isdoubleclick=true

    def judgeclick(self,e):
        if self.isdoubleclick== false:
           self.mouse="左"
        else:
            self.isdoubleclick=false
            self.mouse = "双击"