溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于PyQT如何實現(xiàn)區(qū)分左鍵雙擊和單擊

發(fā)布時間:2020-07-23 14:32:32 來源:億速云 閱讀:214 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了基于PyQT如何實現(xiàn)區(qū)分左鍵雙擊和單擊,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

 在PyQt中沒有直接提供左鍵雙擊的判斷方法,需要自己實現(xiàn),其思路主要如下所示:

1、起動一個定時器,判斷在指定的時間之內(nèi),點擊次數(shù)超過2次,則視為雙擊(其主要思路判斷兩次點擊的時間差在預(yù)測的條件以內(nèi))

2、 起動一個定時器,判斷在指定的時間之內(nèi),點擊次數(shù)超過2次,另外再獲取鼠標點擊的坐標,如果前后兩次點擊的坐標位置,屬于同一個位置,滿足這兩個條件則判斷為雙擊(其主要思路判斷兩次點擊的時間差在預(yù)測的條件以內(nèi),且點擊的坐標在預(yù)設(shè)的坐標之內(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 = '中'
    # 左右鍵同時按下
    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)

看完上述內(nèi)容,是不是對基于PyQT如何實現(xiàn)區(qū)分左鍵雙擊和單擊有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI