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上的文本随着鼠标在不同的位置点击、释放以及左击拖动鼠标的不同而显示相应的文本

如图:

________________________________________________________________________

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()函数。

如图:

_______________________________________________________________________________________________________

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的所有事件,程序运行界面如图:

__________________________________________________________________________________________________________

Qt事件处理04

pyQt事件处理的更多相关文章

  1. Python+Qt学习随笔:PyQt中常用的事件处理函数

    在PyQt图形界面中,我们经常要捕获特定事件如鼠标按键按下.鼠标按下等事件以执行特定操作,可以通过重写组件对象的相关事件处理函数来实现相关处理,具体特定事件常用的包括如下: keyPressEvent ...

  2. Python+Qt学习随笔:PyQt图形界面应用的事件处理流程

    图形界面的事件处理是界面操作的核心,经过编写测试程序验证,基本确认PyQt图形界面应用程序的事件处理流程如下: 1.操作系统或其他应用发送消息给应用主程序: 2.应用主程序调用notify将消息队列中 ...

  3. 第一个PyQt程序

    这个程序虽然小,具备pyqt程序的皱型,可以作为一个模板使用了 #!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtW ...

  4. pyqt中使用matplotlib绘制动态曲线

    一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...

  5. pyqt显示指定范围的数字

    # -*- coding: cp936 -*- # -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui   #导入模块 a ...

  6. pyqt中使用matplotlib绘制动态曲线 – pythonic

    一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...

  7. PyQt4 的事件与信号 -- 重写事件处理方法

    # PyQt中的事件处理主要依赖重写事件处理函数来实现 import sys from PyQt4 import QtCore, QtGui class MainWindow(QtGui.QWidge ...

  8. 关于eric4和pyqt的入门学习(转)

    在Eric4下用PyQt4编写Python的图形界面程序 转载请注明作者RunningOn 本文是PyQt4的入门教程.网上能搜到其它教程,但我觉得讲得不是很清楚,希望这篇文章对入门者更加有帮助. 先 ...

  9. 从Qt到PyQt

    Hello World PyQt与Qt具有极其相似的类族和API,而且不再使用qmake系统和Q_OBJECT宏使得PyQt在没有编译链接时频繁的错误而且代码更加友好. from PyQt4 impo ...

随机推荐

  1. Perl 多线程模块 Parallel::ForkManager

    Perl 多线程模块 Parallel::ForkManager 一个简单的并行处理模块.这个是用来对付循环的多线程处理. 放在循环前面. Table of Contents 1 Synops内容简介 ...

  2. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  3. MDM 证书申请流程(vendor及customer)

    整个流程分为两部分:vendor,customer. 一.Vendor 1.成为一个 MDM Vendor 1) 首先你须要拥有一个 Apple Enterprise account($299/年). ...

  4. C++11 lambda 表达式

    C++11 新增了很多特性,lambda 表达式是其中之一,如果你想了解的 C++11 完整特性,建议去这里,这里,这里,还有这里看看.本文作为 5 月的最后一篇博客,将介绍 C++11 的 lamb ...

  5. 高级子查询【weber出品必属精品】

    多列子查询 where条件中出现多列与子查询进行比较 多列子查询分为:成对比较和非成对比较 成对比较: SQL> select ename,sal,job from emp where (dep ...

  6. ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法

    转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...

  7. C#操作Flash动画

    对于在C#开发的过程中没有接触过Flash相关开发的人员来说,没有系统的资料进行学习,那么这篇文档针对于初学者来说是很好的学习DEMO. 本文章中的DEMO实现了C#的COM控件库中本来就带有对fla ...

  8. C#通过生成ini文件,记住用户关闭程序之前的选择+忽略跨线程检查

    1.在类的里面添加 //写配置文件 [DllImport("kernel32")] private static extern long WritePrivateProfileSt ...

  9. Mvc htmlhelper that generates a menu from a controller

    Simple menu system that grabs a list of actions from a single controller and creates an unordered li ...

  10. 学习hadoop

    一.笔记本触摸板关闭方法 1.在windows下有官方驱动. 2.ubuntu下没有 操作方法如下: 1,终端操作 临时禁止触摸板:sudo modprobe -r psmouse 开启触摸板:sud ...