pyQt事件处理

时间:2023-03-09 07:40:03
pyQt事件处理

Qt事件处理01

Qt处理事件的第二种方式:"重新实现QObject::event()函数",通过重新实现event()函数,可以在事件到达特定的事件处理器之前截获并处理他们。这种方法可以用来覆盖已定义事件的默认处理方式,也可以用来处理Qt中尚未定义特定事件处理器的事件。当重新实现event()函数时,如果不进行事件处理,则需要调用基类的event()函数

Qt是事件驱动的, 程序每个动作都是由某个事件所触发。 Qt事件的类型很多,
我们可以通过查看Qt的 manual中的Event System 和 QEvent 来获得各个事件的详细信息。
事件来源
Spontaneous events(自发事件)
从系统得到的消息,比如鼠标按键,键盘按键等。Qt事件循环的时候读取这些事件,转化为QEvent后依次处理
Posted events
有Qt或应用程序产生,放入消息队列
QCoreApplication::postEvent()
Sent events
由Qt或应用程序产生,不放入队列,直接被派发和处理
QCoreApplication::sendEvent()
比如考虑重绘事件处理函数 paintEvent(),3种事件都能使得该函数被调用:

当窗口被其他窗口覆盖后,再次重新显示时,系统将产生 spontaneous 事件来请求重绘
当我们调用 update() 时,产生的是 Posted 事件
当我们调用 repaint() 时,产生的是 Sent 事件
事件派发事件循环

from PyQt4.QtGui import  *

from PyQt4.Qt import *

from PyQt4.QtCore import *

import sys

#第一种,自定义控件,使用重新实现特定事件处理器,派生一个组件,重新实现它的事件处理,主要使用mousePressEvent、mouseReleaseEvent以及mouseMoveEvent这三个事件处理

class MyBuuton(QPushButton):

def __init__(self,parent=None):

super(MyBuuton,self).__init__(parent)

def mousePressEvent(self,event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def mouseReleaseEvent(self, event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def mouseMoveEvent(self, event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

QTextCodec.setCodecForTr(QTextCodec.codecForName('utf-8'))

app =QApplication(sys.argv)

x = MyBuuton()

x.setWindowTitle(u'处理器')

x.resize(400,200)

x.show()

app.exec_()

序运行时,Button上的文本随着鼠标在不同的位置点击、释放以及左击拖动鼠标的不同而显示相应的文本

如图:pyQt事件处理

________________________________________________________________________

Qt事件处理02

Qt处理事件的第二种方式:"重新实现QObject::event()函数",通过重新实现event()函数,可以在事件到达特定的事件处理器之前截获并处理他们。这种方法可以用来覆盖已定义事件的默认处理方式,也可以用来处理Qt中尚未定义特定事件处理器的事件。当重新实现event()函数时,如果不进行事件处理,则需要调用基类的event()函数。

# -*- coding: utf-8 -*-

# python:2.x

__author__ = 'Administrator'

"""

Qt是事件驱动的, 程序每个动作都是由某个事件所触发。 Qt事件的类型很多,

我们可以通过查看Qt的 manual中的Event System 和 QEvent 来获得各个事件的详细信息。

事件来源

Spontaneous events(自发事件)

从系统得到的消息,比如鼠标按键,键盘按键等。Qt事件循环的时候读取这些事件,转化为QEvent后依次处理

Posted events

有Qt或应用程序产生,放入消息队列

QCoreApplication::postEvent()

Sent events

由Qt或应用程序产生,不放入队列,直接被派发和处理

QCoreApplication::sendEvent()

比如考虑重绘事件处理函数 paintEvent(),3种事件都能使得该函数被调用:

当窗口被其他窗口覆盖后,再次重新显示时,系统将产生 spontaneous 事件来请求重绘

当我们调用 update() 时,产生的是 Posted 事件

当我们调用 repaint() 时,产生的是 Sent 事件

事件派发事件循环

"""

from PyQt4.QtGui import  *

from PyQt4.Qt import *

from PyQt4.QtCore import *

import sys

#第一种,自定义控件,使用重新实现特定事件处理器,派生一个组件,重新实现它的事件处理,主要使用mousePressEvent、mouseReleaseEvent以及mouseMoveEvent这三个事件处理

class MyBuuton(QPushButton):

def __init__(self,parnet=None):

super(MyBuuton,self).__init__(parnet)

def mousePressEvent(self,event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def mouseReleaseEvent(self, event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def mouseMoveEvent(self, event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def event(self, e):#看下&&

if e.type()==QEvent.MouseButtonPress:

event=e#(需要写成这样)

#而不是写成event=类名(e)

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

return True

elif e.type()==QEvent.MouseButtonPress or e.type()==QEvent.MouseMove:#屏蔽MouseButtonRelease和MouseMove事件

return True

return QPushButton.event(self,e)#其他事件调用基类的event()函数进行处理

#不要写成QPushButton.event(e),会报错

QTextCodec.setCodecForTr(QTextCodec.codecForName('utf-8'))

app =QApplication(sys.argv)

x = MyBuuton()

x.setWindowTitle(u'处理器')

x.resize(400,200)

x.show()

app.exec_()

#&&我会在原来的代码上面重写这个方法,需要重载:QObject::event(),通过函过重新实现event()函数,可以在事件到达特定的事件处理器之前截获并处理他们。这种方法可以用来覆盖已定义事件的默认处理方式,也可以用来处理Qt中尚未定义特定事件处理器的事件。当重新实现event()函数时,如果不进行事件处理,则需要调用基类的event()函数。

如图:pyQt事件处理

_______________________________________________________________________________________________________

Qt事件处理03

# -*- coding: utf-8 -*-

# python:2.x

__author__ = 'Administrator'

#Qt处理事件的第三种方式:"在QObject中注册事件过滤器",如果对象使用installEventFilter()函数注册了事件过滤器,目标对象中的所有事件将首先发给这个监视对象的eventFilter()函数

from PyQt4.QtGui import  *

from PyQt4.Qt import *

from PyQt4.QtCore import *

import sys

class MyBuuton(QPushButton):

def __init__(self,parnet=None):

super(MyBuuton,self).__init__(parnet)

def mousePressEvent(self,event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def mouseReleaseEvent(self, event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def mouseMoveEvent(self, event):

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

def event(self, e):#看下&&

if e.type()==QEvent.MouseButtonPress:

event=e

self.setText(QString('x:%1,y:%2').arg(QString.number(event.x())).arg(QString.number(event.y())))

return True

elif e.type()==QEvent.MouseButtonPress or e.type()==QEvent.MouseMove:#屏蔽MouseButtonRelease和MouseMove事件

return True

return QPushButton.event(self,e)#其他事件调用基类的event()函数进行处理

class MyBuuton1(QMainWindow):

def __init__(self,parnet=None):

super(MyBuuton1,self).__init__(parnet)

self.button=MyBuuton()

self.setCentralWidget(self.button)

self.button.installEventFilter(self)#安装过滤器

def eventFilter(self, obj,e):

if obj==self.button:

if e.type()==QEvent.MouseButtonRelease or e.type()==QEvent.MouseMove:#屏蔽MouseButtonRelease和MouseMove事件

return True

else:

return False

return QMainWindow.eventFilter(self,obj,e)#将事件交给上层对话框

QTextCodec.setCodecForTr(QTextCodec.codecForName('utf-8'))

app =QApplication(sys.argv)

x = MyBuuton1()

x.setWindowTitle(u'处理器')

x.resize(400,200)

x.show()

app.exec_()

#运行程序,可以发现button的文本不管是点击、释放还是拖动鼠标,都只显示鼠标按下的文本,因为我们已经为button注册了事件过滤器,在监视对象MainWindow中,事件处理函数eventFilter()函数屏蔽了button的MouseButtonRelease和MouseMove事件。所以目标对象button的MouseButtonRelease和MouseMove事件得不到响应。

#故事件是先经过监视对象的eventFilter()函数,然后在响应目标对象button的所有事件,程序运行界面如图:pyQt事件处理

__________________________________________________________________________________________________________

Qt事件处理04