PyQTに基づいて、左クリックと左クリックを区別することができます。
PyQtでは左クリックの判定方法が直接提供されていません。自分で実現する必要があります。その考えは主に以下の通りです。
1、一つのタイマーを起動して、指定された時間内にクリック回数が2回を超えると判断したら、ダブルクリックと見なします。
2、タイマーを起動し、指定した時間内にクリック回数が2回を超えたと判断し、またマウスクリックの座標を取得し、前後2回クリックした座標位置が同じ位置に属する場合、この2つの条件を満たすとダブルクリックと判断します。一定の偏差があることを許可します。
1、一つのタイマーを起動して、指定された時間内にクリック回数が2回を超えると判断したら、ダブルクリックと見なします。
2、タイマーを起動し、指定した時間内にクリック回数が2回を超えたと判断し、またマウスクリックの座標を取得し、前後2回クリックした座標位置が同じ位置に属する場合、この2つの条件を満たすとダブルクリックと判断します。一定の偏差があることを許可します。
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 = " "
または
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.mouse = ""
self.timer=QTimer(self)
self.timer.timeout.connect(self.singleClicked)
def singleClicked(self):
if self.timer.isActive():
self.timer.stop()
self.mouse=" "
def mouseDoubleClickEvent(self,e):
if self.timer.isActive() and e.buttons() ==QtCore.Qt.LeftButton:
self.timer.stop()
self.mouse=" "
super(myWidgets,self).mouseDoubleClickEvent(e)
def mousePressEvent(self,e):
if e.buttons()== QtCore.Qt.LeftButton:
self.timer.start(1000)
elif e.buttons()== QtCore.Qt.RightButton:
self.mouse=" "
super(myWidgets,self).mousePressEvent(e)
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。