赞
踩
- accept() # 自己处理了这个事件, 并告诉系统不要再向上层传递
- ignore() # 自己忽略了这个事件, 告诉系统, 继续往后传递去
简单版事件机制图
notify(receiver,evt)
方法负责事件的分发,发送给对应的对象进行处理- mousePressEvent(evt) # 鼠标按下事件
- mouseReleaseEvent(evt) # 鼠标释放事件
- mouseClickEvent(evt) # 鼠标单击事件
- .
- .
- .
-
- 比如
- QPushButton控件的mouseClickEvent(evt)事件函数(方法)中就发射了clicked信号
- 我们就可以通过这个信号来连接槽函数
- btn.clicked.connect(槽函数)
-
个人理解
- 接收对象(receiver) ——> 按钮(QPushButton对象)
- evt ——> 鼠标单击事件
通过代码演示Qt事件机制
- import sys
- from PyQt5.Qt import *
-
- app = QApplication(sys.argv)
-
- window = QWidget()
-
- btn1 = QPushButton(window)
- btn1.setText('登录')
- btn1.move(100, 100)
-
- btn2 = QPushButton(window)
- btn2.setText('取消')
- btn2.move(100, 150)
-
- window.show()
- sys.exit(app.exec_())
- import sys
- from PyQt5.Qt import
-
- app = QApplication(sys.argv)
-
- window = QWidget()
-
- btn1 = QPushButton(window)
- btn1.setText('登录')
- btn1.move(100, 100)
-
-
- btn2 = QPushButton(window)
- btn2.setText('取消')
- btn2.move(100, 150)
-
- def cao1():
- print(f'用户按下了【登录】按钮—信号与槽机制')
-
- def cao2():
- print(f'用户按下了【取消】按钮—信号与槽机制')
-
- btn1.pressed.connect(cao1)
- btn2.pressed.connect(cao2)
-
- window.show()
- sys.exit(app.exec_())
- import sys
- from PyQt5.Qt import *
-
-
- class QApp(QApplication):
- pass
-
-
- app = QApp(sys.argv)
-
- window = QWidget()
-
- btn1 = QPushButton(window)
- btn1.setText('登录')
- btn1.move(100, 100)
-
-
- btn2 = QPushButton(window)
- btn2.setText('取消')
- btn2.move(100, 150)
-
- def cao1():
- print(f'用户按下了【登录】按钮—信号与槽机制')
-
- def cao2():
- print(f'用户按下了【取消】按钮—信号与槽机制')
-
- btn1.pressed.connect(cao1)
- btn2.pressed.connect(cao2)
-
- window.show()
- sys.exit(app.exec_())
- import sys
- from PyQt5.Qt import *
-
-
- class QApp(QApplication):
- def notify(self, receiver, evt):
- print('QApp对象里面的notify方法',receiver, evt)
- return super().notify(receiver, evt)
-
-
- app = QApp(sys.argv)
-
- window = QWidget()
-
- btn1 = QPushButton(window)
- btn1.setText('登录')
- btn1.move(100, 100)
-
-
- btn2 = QPushButton(window)
- btn2.setText('取消')
- btn2.move(100, 150)
-
- def cao1():
- print(f'用户按下了【登录】按钮—信号与槽机制')
-
- def cao2():
- print(f'用户按下了【取消】按钮—信号与槽机制')
-
- btn1.pressed.connect(cao1)
- btn2.pressed.connect(cao2)
-
- window.show()
- sys.exit(app.exec_())
此时运行程序,我们发现控制台会不断的打印内容,说明notify()方法在不断执行。是因为操作系统捕获到了很多跟这个应用程序相关的事件消息,就会不断的分发给这个应用程序的QApplication对象的notify()方法
此时我们只需要在接收到QPushButton对象事件消息的时候才打印相应内容,那么我们就需要对事件接收者对象(receiver)进行判断
- class QApp(QApplication):
- def notify(self, receiver, evt):
- if receiver.inherits('QPushButton'):
- print('QApp对象里面的notify方法',receiver, evt)
- return super().notify(receiver, evt)
此时再运行的时候,就只有QPushButton对象的事件消息才会触发notify()方法中的print()方法进行打印了
此时接收到的QPushButton对象的所有事件消息都会触发这个打印,那么我们只需要在QPushButton的按下事件(MouseButtonPress)时才触发,那么我们就要继续对事件对象(evt)的类型进行判断了
- class QApp(QApplication):
- def notify(self, receiver, evt):
- if receiver.inherits('QPushButton') and evt.type() == QEvent.MouseButtonPress:
- print('QApp对象里面的notify方法',receiver, evt)
- return super().notify(receiver, evt)
此时,我们再运行程序,就只有鼠标按下按钮的时候才会触发notify()方法中的打印了
- class QApp(QApplication):
- def notify(self, receiver, evt):
- if receiver.inherits('QPushButton') and evt.type() == QEvent.MouseButtonPress:
- print('QApp对象里面的notify方法',receiver, evt)
- else:
- return super().notify(receiver, evt)
- import sys
- from PyQt5.Qt import *
-
-
- class QApp(QApplication):
- def notify(self, receiver, evt):
- if receiver.inherits('QPushButton') and evt.type() == QEvent.MouseButtonPress:
- print('QApp对象里面的notify方法',receiver, evt)
-
- return super().notify(receiver, evt)
-
- class Btn(QPushButton):
- pass
-
- app = QApp(sys.argv)
-
- window = QWidget()
-
- btn1 = Btn(window)
- btn1.setText('登录')
- btn1.move(100, 100)
-
-
- btn2 = Btn(window)
- btn2.setText('取消')
- btn2.move(100, 150)
-
- def cao1():
- print(f'用户按下了【登录】按钮—信号与槽机制')
-
- def cao2():
- print(f'用户按下了【取消】按钮—信号与槽机制')
-
- btn1.pressed.connect(cao1)
- btn2.pressed.connect(cao2)
-
- window.show()
- sys.exit(app.exec_())
- class Btn(QPushButton):
- def event(self, evt) -> bool:
- print('Btn中的event方法', evt)
- return super(Btn, self).event(evt)
- class Btn(QPushButton):
- def event(self, evt):
- if evt.type() == QEvent.MouseButtonPress:
- print('Btn中的event方法', evt)
- return super().event(evt)
- class Btn(QPushButton):
- def event(self, evt):
- if evt.type() == QEvent.MouseButtonPress:
- print('Btn中的event方法', evt)
- return super().event(evt)
- def mousePressEvent(self, evt):
- print('Btn中的MouseButtonPress方法', evt)
- class Btn(QPushButton):
- def event(self, evt):
- if evt.type() == QEvent.MouseButtonPress:
- print('Btn中的event方法', evt)
- return super().event(evt)
- def mousePressEvent(self, evt):
- print('Btn中的MouseButtonPress方法', evt)
- return super().mousePressEvent(evt)
- import sys
- from PyQt5.Qt import *
-
-
- class QApp(QApplication):
- def notify(self, receiver, evt):
- if receiver.inherits('QPushButton') and evt.type() == QEvent.MouseButtonPress:
- print('QApp对象里面的notify方法',receiver, evt)
-
- return super().notify(receiver, evt)
-
- class Btn(QPushButton):
- def event(self, evt):
- if evt.type() == QEvent.MouseButtonPress:
- print('Btn中的event方法', evt)
- return super().event(evt)
-
- def mousePressEvent(self, evt):
- print('Btn中的MouseButtonPress方法', evt)
- return super().mousePressEvent(evt)
-
- app = QApp(sys.argv)
-
- window = QWidget()
-
- btn1 = Btn(window)
- btn1.setText('登录')
- btn1.move(100, 100)
-
-
- btn2 = QPushButton(window)
- btn2.setText('取消')
- btn2.move(100, 150)
-
- def cao1():
- print(f'用户按下了【登录】按钮—信号与槽机制')
-
- def cao2():
- print(f'用户按下了【取消】按钮—信号与槽机制')
-
- btn1.pressed.connect(cao1)
- btn2.pressed.connect(cao2)
-
- window.show()
- sys.exit(app.exec_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。