【PyQt4 实例27】事件过滤器

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class EventFilter(QDialog):
    def __init__(self,parent=None):
        super(EventFilter,self).__init__(parent)
        self.setWindowTitle(self.tr("事件过滤器"))
        
        self.label1 = QLabel(self.tr("请点击"))
        self.label2 = QLabel(self.tr("请点击"))
        self.label3 = QLabel(self.tr("请点击"))
        self.LabelState = QLabel(self.tr("test"))
        
        self.image1 = QImage("image/butterfly.png")
        self.image2 = QImage("image/butterfly.png")
        self.image3 = QImage("image/butterfly.png")
        
        self.width = 600
        self.height = 300

        self.resize(self.width,self.height)
        
        self.label1.installEventFilter(self)
        self.label2.installEventFilter(self)
        self.label3.installEventFilter(self)
        
        mainLayout = QGridLayout(self)
        mainLayout.addWidget(self.label1,500,0)
        mainLayout.addWidget(self.label2,500,1)
        mainLayout.addWidget(self.label3,500,2)
        mainLayout.addWidget(self.LabelState,600,1)
        self.setLayout(mainLayout)
        
    def eventFilter(self,watched,event):
        if watched == self.label1:
            if event.type() == QEvent.MouseButtonPress:
                mouseEvent = QMouseEvent(event)
                if mouseEvent.buttons() == Qt.LeftButton:
                    self.LabelState.setText(self.tr("Left mouse button pressed on left image"))
                elif mouseEvent.buttons() == Qt.MidButton:
                    self.LabelState.setText(self.tr("Middle mouse button pressed on left image"))
                elif mouseEvent.buttons() == Qt.RightButton:
                    self.LabelState.setText(self.tr("Right mouse button pressed on left image"))
                matrix = QMatrix()
                matrix.scale(0.8,0.8)
                tmp = self.image1.transformed(matrix)
                self.label1.setPixmap(QPixmap.fromImage(tmp))
            if event.type() == QEvent.MouseButtonRelease:
                self.LabelState.setText(self.tr("Mouse button released from left image"))
                self.label1.setPixmap(QPixmap.fromImage(self.image1))
        return QDialog.eventFilter(self,watched,event)
    
app=QApplication(sys.argv)
dialog=EventFilter()
dialog.show()
app.exec_()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值