基于PyQT實(shí)現(xiàn)區(qū)分左鍵雙擊和單擊
在PyQt中沒有直接提供左鍵雙擊的判斷方法,需要自己實(shí)現(xiàn),其思路主要如下所示:
1、起動一個(gè)定時(shí)器,判斷在指定的時(shí)間之內(nèi),點(diǎn)擊次數(shù)超過2次,則視為雙擊(其主要思路判斷兩次點(diǎn)擊的時(shí)間差在預(yù)測的條件以內(nèi))
2、 起動一個(gè)定時(shí)器,判斷在指定的時(shí)間之內(nèi),點(diǎn)擊次數(shù)超過2次,另外再獲取鼠標(biāo)點(diǎn)擊的坐標(biāo),如果前后兩次點(diǎn)擊的坐標(biāo)位置,屬于同一個(gè)位置,滿足這兩個(gè)條件則判斷為雙擊(其主要思路判斷兩次點(diǎn)擊的時(shí)間差在預(yù)測的條件以內(nèi),且點(diǎn)擊的坐標(biāo)在預(yù)設(shè)的坐標(biāo)之內(nèi),允許存在一定的偏差)
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 = '中' # 左右鍵同時(shí)按下 elif e.buttons() == QtCore.Qt.LeftButton | QtCore.Qt.RightButton: self.mouse = '左右' # 左中鍵同時(shí)按下 elif e.buttons() == QtCore.Qt.LeftButton | QtCore.Qt.MidButton: self.mouse = '左中' # 右中鍵同時(shí)按下 elif e.buttons() == QtCore.Qt.MidButton | QtCore.Qt.RightButton: self.mouse = '右中' # 左中右鍵同時(shí)按下 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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬蟲實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法
下面小編就為大家分享一篇Python爬蟲實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

echarts動態(tài)獲取Django數(shù)據(jù)的實(shí)現(xiàn)示例

python人工智能算法之差分進(jìn)化算法的實(shí)現(xiàn)

Python使用multiprocessing如何實(shí)現(xiàn)多進(jìn)程

Python基于pyecharts實(shí)現(xiàn)關(guān)聯(lián)圖繪制

如何解決cmd運(yùn)行python提示不是內(nèi)部命令

Python爬取肯德基官網(wǎng)ajax的post請求實(shí)現(xiàn)過程