# -*- 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_()
【PyQt4 实例27】事件过滤器
最新推荐文章于 2024-05-13 09:38:24 发布