重定義鼠標響應+鍵盤響應事件
一,每個事件都被封裝成相應的類:
pyqt中,每個事件類型都被封裝成相應的事件類,如鼠標事件為QMouseEvent,鍵盤事件為QKeyEvent等。而它們的基類是QEvent。
二,基類QEvent的幾個重要方法:
accept() 表示事件已處理,不需要向父窗口傳播
ignore()表示事件未處理,繼續向父窗口傳播f
type()返回事件類型,如QtCore.QEvent.MouseButtonPress,一般由基事件調用。因為其它事件已經知道自己的事件類型了。
還有一個自定義事件的注冊方法。
三,QMouseEvent鼠標事件:
buttons() 返回哪個鼠標按鍵被按住了。如Qt.LeftButton
globalPos() 返回鼠標相對屏幕的位置QPoint
pos() 返回鼠標相對處理事件的窗口的位置
四、處理鼠標事件的響應函數(在QWidget及其繼承類中):
mousePressEvent(QMouseEvent) #鼠標點擊觸發事件
mouseReleaseEvent(event) #鼠標釋放觸發事件
mouseMoveEvent(event) #鼠標移動觸發事件
# 事件。 """重寫鼠標事件,實現窗口拖動。""" def mousePressEvent(self, event): if event.buttons() == Qt.LeftButton: self.setCursor(Qt.OpenHandCursor) self.parent.m_drag = True self.parent.m_DragPosition = event.globalPos()-self.parent.pos() event.accept() def mouseMoveEvent(self, event): try: if event.buttons() and Qt.LeftButton: self.parent.move(event.globalPos()-self.parent.m_DragPosition)#move將窗口移動到指定位置 event.accept() except AttributeError: pass def mouseReleaseEvent(self, event): if event.button()==Qt.LeftButton: self.m_drag = False self.unsetCursor()
效果如下:
重新定義鼠標事件:
"""重定義鼠標單擊事件""" def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.lab1.setText("鼠標左鍵點擊!") # print(event.pos().x(),event.pos().y()) if event.button() == Qt.RightButton: self.lab1.setText("鼠標右鍵點擊!") """當鼠標左鍵點擊拖動時觸發事件,有無if判斷條件效果都一樣""" def mouseMoveEvent(self, event): # if event.buttons() == Qt.LeftButton: # # print(type(event.pos().x())) #<class 'int'> # self.lab2.setText(str(event.pos().x())+","+str(event.pos().y())) self.pos = event.pos() print(self.pos) self.lab2.setText(str(event.pos().x()) + "," + str(event.pos().y())) self.update()
完整代碼:

1 from PyQt5.QtCore import Qt 2 from PyQt5.QtGui import (QPainter, QColor, QPen) 3 import sys 4 from PyQt5.QtWidgets import (QApplication,QWidget,QLabel) 5 6 class Example(QWidget): 7 def __init__(self): 8 super(Example, self).__init__() 9 self.initUi() 10 #默認情況下禁用鼠標跟蹤, 如果啟用鼠標跟蹤,即使沒有按鈕被按下,小部件也會接收鼠標移動事件。 11 #當然你也可以不寫,只需要在執行的過程中按照鼠標左鍵也行 12 self.setMouseTracking(True) 13 14 def initUi(self): 15 self.setGeometry(400,300,400,300) 16 self.setWindowTitle("鍵盤響應事件") 17 self.lab1 = QLabel("方向",self) 18 self.lab1.setGeometry(200,150,100,100) 19 self.lab2 = QLabel("顯示鼠標坐標", self) 20 self.lab2.setGeometry(200, 80, 100, 100) 21 22 """重定義鍵盤事件""" 23 def keyPressEvent(self,e ): 24 if e.key() == Qt.Key_Up: 25 self.lab1.setText("↑") 26 elif e.key() == Qt.Key_Down: 27 self.lab1.setText("↓") 28 elif e.key() == Qt.Key_Left: 29 self.lab1.setText("←") 30 else: 31 self.lab1.setText("→") 32 33 """重定義鼠標單擊事件""" 34 def mousePressEvent(self, event): 35 if event.button() == Qt.LeftButton: 36 self.lab1.setText("鼠標左鍵點擊!") 37 # print(event.pos().x(),event.pos().y()) 38 if event.button() == Qt.RightButton: 39 self.lab1.setText("鼠標右鍵點擊!") 40 41 """當鼠標左鍵點擊拖動時觸發事件,有無if判斷條件效果都一樣""" 42 def mouseMoveEvent(self, event): 43 # if event.buttons() == Qt.LeftButton: 44 # # print(type(event.pos().x())) #<class 'int'> 45 # self.lab2.setText(str(event.pos().x())+","+str(event.pos().y())) 46 self.pos = event.pos() 47 print(self.pos) 48 self.lab2.setText(str(event.pos().x()) + "," + str(event.pos().y())) 49 self.update() 50 51 52 53 if __name__ == '__main__': 54 app = QApplication(sys.argv) 55 ex = Example() 56 ex.show() 57 sys.exit(app.exec_())
所有的QT鍵盤事件代碼如下:
https://pan.baidu.com/s/1Brry6fkUcxaP-uOdukD8Ng