PyQt事件处理
- 前言
- 1 低级的事件处理程序
- 2 事件处理机制
- 1、事件机制与信号槽机制的区别
- 2、事件处理的方法
- 3、事件处理实例
前言
1 低级的事件处理程序
'''
【简介】
PyQT5中实时刷新界面例子
'''
from PyQt5.QtWidgets import QWidget, QPushButton , QApplication ,QListWidget, QGridLayout
import sys
import time
class WinForm(QWidget):
def __init__(self,parent=None):
super(WinForm,self).__init__(parent)
self.setWindowTitle("实时刷新界面例子")
self.listFile= QListWidget()
self.btnStart = QPushButton('开始')
layout = QGridLayout(self)
layout.addWidget(self.listFile,0,0,1,2)
layout.addWidget(self.btnStart,1,1)
self.btnStart.clicked.connect( self.slotAdd)
self.setLayout(layout)
def slotAdd(self):
for n in range(10):
str_n='File index {0}'.format(n)
self.listFile.addItem(str_n)
QApplication.processEvents()
time.sleep(1)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
2 事件处理机制
1、事件机制与信号槽机制的区别
PyQt为事件处理提供了高级别的信号槽机制和低级别的事件处理机制,信号槽机制是事件处理机制的高级封装。使用控件时,不用考虑事件处理机制,只需要关心信号槽即可;对于自定义派生控件,必须考虑事件处理机制,根据控件的行为需求重新实现相应的事件处理函数。
2、事件处理的方法
PyQt提供了5种事件处理和过滤方法,分别为:
(1)重新实现事件处理函数
常用的事件处理函数如paintEvent、mouseMoveEvent、mousePressEvent、mouseReleaseEvent等。
(2)重新实现QObject.event事件分发函数
在增加新的事件时,需要重新实现QObject.event方法,并增加新事件的分发路由。
(3)安装事件过滤器
如果对QObject对象调用installEventFilter方法,则为QObject对象安装事件过滤器。QObject对象的所有事件都会先传递到事件过滤器eventFilter函数,在事件过滤器eventFilter函数中可以丢弃或修改某些事件,对感兴趣的事件使用自定义的事件处理机制,对其它事件使用默认事件处理机制。事件过滤机制会对QObject的所有事件进行过滤,因此如果要过滤的事件比较多则会影响程序性能。
(4)在QApplication安装事件过滤器
在QApplication对象安装事件过滤器将会对所有QObject对象的所有事件进行过滤,并且会首先获得事件,即将事件发送给其它任何一个事件过滤器前,都会首先发送给QApplication的事件过滤器。
(5)重新QApplication的notify方法
PyQt使用QApplication对象的notify方法进行分发事件,要想在任何事件过滤器前捕获事件唯一的方法就是重新实现QApplication的notify方法。
3、事件处理实例
QDialog对话框在ESC按键按下时会自动退出,使用事件处理和过滤对按下ESC按键进行处理。
(1)重新实现事件处理函数
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
def keyPressEvent(self, event):
if event.key() != Qt.Key_Escape:
QDialog.keyPressEvent(self, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = Dialog()
dialog.exec_()
sys.exit(app.exec_())
(2)重新实现event函数
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
def event(self, event):
if event.type() == QKeyEvent.KeyPress and event.key() == Qt.Key_Escape:
return True
else:
return QDialog.event(self, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = Dialog()
dialog.exec_()
sys.exit(app.exec_())
(3)QObject安装事件过滤器
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.installEventFilter(self)
def eventFilter(self, watched, event):
if event.type() == QKeyEvent.KeyPress and event.key() == Qt.Key_Escape:
return True
else:
return QDialog.eventFilter(self, watched, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = Dialog()
dialog.exec_()
sys.exit(app.exec_())
(4)QApplication安装事件过滤器
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
def eventFilter(self, watched, event):
if event.type() == QKeyEvent.KeyPress and event.key() == Qt.Key_Escape:
return True
else:
return QDialog.eventFilter(self, watched, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = Dialog()
app.installEventFilter(dialog)
dialog.exec_()
sys.exit(app.exec_())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)