QT事件过滤(QObject::installEventFilter()和QObject::eventFilter(QObject*,QEvent*)函数)

事件过滤

  • 程序
  • 效果展示

程序

.h
#ifndef EVENTFILTER_H
#define EVENTFILTER_H

#include 
#include 

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
#include "eventfilter.h"
#include 
#include 
#include 
#include 
#include 

EventFilter::EventFilter(QWidget *parent)
    : QDialog(parent)
{
     
    setWindowTitle(tr("事件过滤"));
    label1 = new QLabel;
    Image1.load("D:/Qtc/EventFilter/image/1.png");
    label1->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    label1->setPixmap(QPixmap::fromImage(Image1));
    label2 = new QLabel;
    Image2.load("D:/Qtc/EventFilter/image/2.png");
    label2->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    label2->setPixmap(QPixmap::fromImage(Image2));
    label3 = new QLabel;
    Image3.load("D:/Qtc/EventFilter/image/3.png");
    label3->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    label3->setPixmap(QPixmap::fromImage(Image3));
    stateLabel = new QLabel(tr("鼠标按下标志"));
    stateLabel->setAlignment(Qt::AlignHCenter);
    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);
}

EventFilter::~EventFilter()
{
     

}

bool EventFilter::eventFilter(QObject *watched, QEvent *event)
{
     
    if(watched==label1)
    {
     
        if(event->type()==QEvent::MouseButtonPress)
        {
     
            //将事件event转化为鼠标事件
            QMouseEvent *mouseEvent=(QMouseEvent *)event;
            /* 以下根据鼠标的按键类型分别显示 */
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
     
                stateLabel->setText(tr("左键按下左边图片"));
            }
            else if(mouseEvent->buttons()&Qt::MidButton)
            {
     
                stateLabel->setText(tr("中键按下左边图片"));
            }
            else if(mouseEvent->buttons()&Qt::RightButton)
            {
     
                stateLabel->setText(tr("右键按下左边图片"));
            }
            //显示放大的图片![在这里插入图片描述](https://img-blog.csdnimg.cn/20200420200254720.gif)
            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(tr("鼠标释放左边图片"));
            label1->setPixmap(QPixmap::fromImage(Image1));
        }
    }
    else if(watched==label2)
    {
     
         if(event->type()==QEvent::MouseButtonPress)
         {
     
             //将事件event转化为鼠标事件
             QMouseEvent *mouseEvent=(QMouseEvent *)event;
             /* 以下根据鼠标的按键类型分别显示 */
             if(mouseEvent->buttons()&Qt::LeftButton)
             {
     
                 stateLabel->setText(tr("左键按下中间图片"));
             }
             else if(mouseEvent->buttons()&Qt::MidButton)
             {
     
                 stateLabel->setText(tr("中键按下中间图片"));
             }
             else if(mouseEvent->buttons()&Qt::RightButton)
             {
     
                 stateLabel->setText(tr("右键按下中间图片"));
             }
             /* 显示放大的图片 */
             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(tr("鼠标释放中间图片"));
             label2->setPixmap(QPixmap::fromImage(Image2));
        }
    }
    else if(watched==label3)
    {
     
        if(event->type()==QEvent::MouseButtonPress)
        {
     
            //将事件event转化为鼠标事件
            QMouseEvent *mouseEvent=(QMouseEvent *)event;
            /* 以下根据鼠标的按键类型分别显示 */
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
     
                stateLabel->setText(tr("左键按下右边图片"));
            }
            else if(mouseEvent->buttons()&Qt::MidButton)
            {
     
                stateLabel->setText(tr("中键按下右边图片"));
            }
            else if(mouseEvent->buttons()&Qt::RightButton)
            {
     
                stateLabel->setText(tr("右键按下右边图片"));
            }
            /* 显示放大的图片 */
            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(tr("鼠标释放右边图片"));
            label3->setPixmap(QPixmap::fromImage(Image3));
        }
    }
    //将事件交给上层对话框
    return QDialog::eventFilter(watched,event);
}

效果展示

分别用鼠标左键中键右键点击三个图片
QT事件过滤(QObject::installEventFilter()和QObject::eventFilter(QObject*,QEvent*)函数)_第1张图片

你可能感兴趣的:(QT学习)