QT的事件過濾是經過重寫函數來實現的,這個函數是:eventFilter()函數
如下是一個經過單擊不一樣的標籤,實現標籤上圖片的變化的例子。
this
首先新建一個項目,基於對話框spa
頭文件:code
#ifndef EVENTFILTER_H #define EVENTFILTER_H #include <QDialog> #include <QImage> #include <QLabel> #include <QEvent> class EventFilter : public QDialog { Q_OBJECT public: EventFilter(QWidget *parent = 0); ~EventFilter(); public slots: bool eventFilter(QObject *, QEvent *); private: QLabel *label1; QLabel *label2; QLabel *label3; QLabel *stateLabel; QImage image1; QImage image2; QImage image3; }; #endif // EVENTFILTER_H
.cpp文件:orm
#include "eventfilter.h" #include <QGridLayout> #include <QMouseEvent> EventFilter::EventFilter(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("事件過濾")); label1=new QLabel; image1.load(":/new/prefix1/image/2.png"); label1->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter); label1->setPixmap(QPixmap::fromImage(image1)); label2=new QLabel; image2.load(":/new/prefix1/image/2.png"); label2->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter); label2->setPixmap(QPixmap::fromImage(image2)); label3=new QLabel; image3.load(":/new/prefix1/image/2.png"); label3->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter); label3->setPixmap(QPixmap::fromImage(image3)); stateLabel=new QLabel; stateLabel->setText("鼠標按下標誌"); stateLabel->setAlignment(Qt::AlignCenter); QHBoxLayout *Layout=new QHBoxLayout; Layout->addWidget(label1); Layout->addWidget(label2); Layout->addWidget(label3); QVBoxLayout *mainLayout=new QVBoxLayout(this); mainLayout->addLayout(Layout); mainLayout->addWidget(stateLabel); label1->installEventFilter(this); label2->installEventFilter(this); label3->installEventFilter(this); } bool EventFilter::eventFilter(QObject *watched, QEvent *event) { if(watched==label1) { if(event->type()==QEvent::MouseButtonPress) { QMouseEvent *mouseEvent=(QMouseEvent *)event; { if(mouseEvent->button()==Qt::LeftButton) { stateLabel->setText("左鍵被按下左邊圖片"); } else if(mouseEvent->button()==Qt::RightButton) { stateLabel->setText("右鍵被按下左邊圖片"); } else if(mouseEvent->button()==Qt::MidButton) { stateLabel->setText("中健被按下左邊圖片"); } QMatrix matrix; matrix.scale(1.8,1.8); QImage tmpimg=image1.transformed(matrix); label1->setPixmap(QPixmap::fromImage(tmpimg)); } } if(event->type()==QEvent::MouseButtonRelease) { stateLabel->setText("鼠標釋放左邊圖片"); label1->setPixmap(QPixmap::fromImage(image1)); } } else if(watched==label2) { if(event->type()==QEvent::MouseButtonPress) { QMouseEvent *mouseEvent=(QMouseEvent *)event; { if(mouseEvent->button()==Qt::LeftButton) { stateLabel->setText("左鍵被按下中間圖片"); } else if(mouseEvent->button()==Qt::RightButton) { stateLabel->setText("右鍵被按下中間圖片"); } else if(mouseEvent->button()==Qt::MidButton) { stateLabel->setText("中健被按下中間圖片"); } QMatrix matrix; matrix.scale(1.8,1.8); QImage tmpimg=image2.transformed(matrix); label2->setPixmap(QPixmap::fromImage(tmpimg)); } } if(event->type()==QEvent::MouseButtonRelease) { stateLabel->setText("鼠標釋放中間圖片"); label2->setPixmap(QPixmap::fromImage(image2)); } } else if(watched==label3) { if(event->type()==QEvent::MouseButtonPress) { QMouseEvent *mouseEvent=(QMouseEvent *)event; { if(mouseEvent->button()==Qt::LeftButton) { stateLabel->setText("左鍵被按下右邊圖片"); } else if(mouseEvent->button()==Qt::RightButton) { stateLabel->setText("右鍵被按下右邊圖片"); } else if(mouseEvent->button()==Qt::MidButton) { stateLabel->setText("中健被按下右邊圖片"); } QMatrix matrix; matrix.scale(1.8,1.8); QImage tmpimg=image3.transformed(matrix); label3->setPixmap(QPixmap::fromImage(tmpimg)); } } if(event->type()==QEvent::MouseButtonRelease) { stateLabel->setText("鼠標釋放右邊圖片"); label3->setPixmap(QPixmap::fromImage(image3)); } } return QDialog::eventFilter(watched,event); } EventFilter::~EventFilter() { }運行後的效果:
如圖,經過單擊不一樣的標籤,標籤顯示不一樣的大小的圖片,而且最下的標籤也跟着改變。這裏的實現原理就是事件過濾,過濾標籤上的單擊事件。
事件