鼠標(biāo)事件
- 做GUI編程很常見(jiàn)的一個(gè)需求招刨,要將按鈕或者其他控件的光標(biāo)移入、移出灵份、按壓時(shí)三種情況的控件樣式都不一樣仁堪,有些人把這三種情況叫做控件的三態(tài)
- 另一種情況需要更改光標(biāo)的樣式
光標(biāo)
-
更改鼠標(biāo)移入光標(biāo)
self.btn.setCursor(QCursor(Qt.PointingHandCursor))
參數(shù) 效果 PointingHandCursor 變?yōu)槭中?/td> CrossCursor 變?yōu)槭中?/td> ArrowCursor 變?yōu)榧^型 UpArrowCursor 變?yōu)橄蛏霞^型 IBeamCursor 變?yōu)槲谋据斎胄?/td> WaitCursor 變?yōu)榈却?/td> BusyCursor 變?yōu)榉泵π?/td> ForbiddenCursor 變?yōu)榻剐?/td> WhatsThisCursor 變?yōu)閱?wèn)號(hào)型 SizeVerCursor 變?yōu)榇怪蓖献?/td> SizeHorCursor 變?yōu)樗酵献?/td> SizeBDiagCursor 變?yōu)閷?duì)角線調(diào)整大小型 SizeAllCursor 變?yōu)橐苿?dòng)對(duì)象型 SplitHCursor 變?yōu)樗讲鸱中?/td> SplitVCursor 變?yōu)榇怪辈鸱中?/td> OpenHandCursor 變?yōu)榇蜷_(kāi)型 ClosedHandCursor 變?yōu)殛P(guān)閉型 BlankCursor 變?yōu)榭瞻仔?/td>
鼠標(biāo)移入事件
-
如果想要將控件的鼠標(biāo)移入或者移出綁定函數(shù)、搞自己的功能填渠,那就得寫(xiě)一個(gè)類(lèi)繼承此控件(QLable弦聂、QPushButton)然后重寫(xiě)此類(lèi)的相關(guān)方法
class MyBtn(QPushButton): def __init__(self, text): super().__init__(text) def mouseMoveEvent(self, e): print("ok") def mousePressEvent(self, e): print("Not Ok") def enterEvent(self, a0): print("123") return super().enterEvent(a0) def leaveEvent(self, a0): print("321") return super().leaveEvent(a0)
-
相關(guān)事件總結(jié)(由于個(gè)人時(shí)間有限無(wú)法全部查找測(cè)試其功能,這里整理了常用的一部分氛什,都是簡(jiǎn)單英文組合莺葫,應(yīng)該不難懂)
事件 作用 enterEvent 鼠標(biāo)移入觸發(fā) leaveEvent 鼠標(biāo)離開(kāi)觸發(fā) mouseDoubleClickEvent 鼠標(biāo)雙擊點(diǎn)擊觸發(fā) mouseMoveEvent 鼠標(biāo)移動(dòng)觸發(fā) mousePressEvent 鼠標(biāo)按壓觸發(fā) mouseReleaseEvent 鼠標(biāo)釋放觸發(fā) keyPressEvent 鍵盤(pán)按下觸發(fā) keyReleaseEvent 鍵盤(pán)釋放觸發(fā) closeEvent 關(guān)閉時(shí)觸發(fā) resizeEvent 設(shè)置大小時(shí)觸發(fā) showEvent 顯示控件時(shí)觸發(fā) dragLeaveEvent dragMoveEvent dropEvent focusInEvent focusOutEvent hideEvent inputMethodEvent installEventFilter customEvent dragEvent actionEvent changeEvent childEvent contextMenuEvent moveEvent nativeEvent paintEvent removeEventFliter tabletEvent timerEvent wheelEvent
-
詳細(xì)代碼
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout from PyQt5.QtGui import QCursor, QMouseEvent from PyQt5.QtCore import Qt import sys class testWindow(QWidget): def __init__(self): super().__init__() self.btn1 = QPushButton("btn1") self.btn2 = QPushButton("btn2") self.btn3 = MyBtn(text="btn3") self.btn1.setCursor(QCursor(Qt.PointingHandCursor)) self.btn2.setCursor(QCursor(Qt.CrossCursor)) self.btn3.event self.layout = QVBoxLayout() self.layout.addWidget(self.btn1) self.layout.addWidget(self.btn2) self.layout.addWidget(self.btn3) self.setLayout(self.layout) def test(self): print("ok") class MyBtn(QPushButton): def __init__(self, text): super().__init__(text) def mouseMoveEvent(self, e): print("ok") def mousePressEvent(self, e): print("Not Ok") def enterEvent(self, a0): print("123") return super().enterEvent(a0) def leaveEvent(self, a0): print("321") return super().leaveEvent(a0) if __name__ == "__main__": app = QApplication(sys.argv) w = testWindow() w.show() sys.exit(app.exec_())