当前位置:   article > 正文

Qt之事件过滤—筛选处理对象_事件过滤的概念是什么

事件过滤的概念是什么

事件过滤

事件过滤是当事件发生时,可以对不同对象,实现不同操作,以达到筛选的效果。
步骤:
1、首先安装一个事件过滤器,为对象安装事件过滤,指定“谁”来监控这些事件对象

//给m_label123安装事件过滤,指定(this)当前整个窗体监视我们的事件对象
m_label1->installEventFilter(this);
m_label2->installEventFilter(this);
m_label3->installEventFilter(this);
  • 1
  • 2
  • 3
  • 4

2、重写bool eventFilter(QObject *obj, QEvent *event)。

//第一个参数是监视对象,第二个参数是事件
bool EventFilter::eventFilter(QObject *watched, QEvent *event)
{
    QMatrix matrix;
    QImage tmpImg;
    //放大图片
    matrix.scale(2.0,2.0);

    if(watched == m_label1)
    {
        //鼠标按下事件
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                m_stateLabel->setText(QStringLiteral("左键按下图片1"));
            }
            if(mouseEvent->buttons()&Qt::MidButton)
            {
                m_stateLabel->setText(QStringLiteral("中键按下图片1"));
            }
            if(mouseEvent->buttons()&Qt::RightButton)
            {
                m_stateLabel->setText(QStringLiteral("右键按下图片1"));
            }

            tmpImg = m_image1.transformed(matrix);
            m_label1->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type()==QEvent::MouseButtonRelease)
        {
            m_stateLabel->setText(QStringLiteral("鼠标释放图片1"));
            m_label1->setPixmap(QPixmap::fromImage(m_image1));
        }
    }
    else if(watched == m_label2)
    {
        //鼠标按下事件
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                m_stateLabel->setText(QStringLiteral("左键按下图片2"));
            }
            if(mouseEvent->buttons()&Qt::MidButton)
            {
                m_stateLabel->setText(QString("中键按下图片2"));
            }
            if(mouseEvent->buttons()&Qt::RightButton)
            {
                m_stateLabel->setText(QString("右键按下图片2"));
            }

            tmpImg = m_image2.transformed(matrix);
            m_label2->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type()==QEvent::MouseButtonRelease)
        {
            m_stateLabel->setText(QStringLiteral("鼠标释放图片2"));
            m_label2->setPixmap(QPixmap::fromImage(m_image2));
        }
    }
    else if(watched == m_label3)
    {
        //鼠标按下事件
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                m_stateLabel->setText(QStringLiteral("左键按下图片3"));
            }
            if(mouseEvent->buttons()&Qt::MidButton)
            {
                m_stateLabel->setText(QStringLiteral("中键按下图片3"));
            }
            if(mouseEvent->buttons()&Qt::RightButton)
            {
                m_stateLabel->setText(QStringLiteral("右键按下图片3"));
            }

            tmpImg = m_image3.transformed(matrix);
            m_label3->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type()==QEvent::MouseButtonRelease)
        {
            m_stateLabel->setText(QStringLiteral("鼠标释放图片3"));
            m_label3->setPixmap(QPixmap::fromImage(m_image3));
        }
    }

    //将事件交给上层对话框进行处理
    return QDialog::eventFilter(watched,event);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

完整代码

eventfilter.h

#ifndef EVENTFILTER_H
#define EVENTFILTER_H

#include <QDialog>
#include <QLabel>
class EventFilter : public QDialog
{
    Q_OBJECT

public:
    EventFilter(QWidget *parent = 0,Qt::WindowFlags f = 0);
    ~EventFilter();
public slots:
    //第一个参数是监视的对象,第二个参数是事件
    bool eventFilter(QObject*watched,QEvent *event)override;
private:
    //将图片加载到label中
    QLabel* m_label1;
    QLabel* m_label2;
    QLabel* m_label3;
    QLabel* m_stateLabel;
    //
    QImage m_image1;
    QImage m_image2;
    QImage m_image3;
};

#endif // EVENTFILTER_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

eventfilter.cpp

#include "eventfilter.h"
#include <QPixmap>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QEvent>
#include <QMouseEvent>
EventFilter::EventFilter(QWidget *parent,Qt::WindowFlags f)
    : QDialog(parent,f)
{
    setWindowTitle(QStringLiteral("事件过滤"));

    m_label1 = new QLabel;
    m_label2 = new QLabel;
    m_label3 = new QLabel;

    m_stateLabel = new QLabel(QStringLiteral("鼠标按下标志"));
    //加个框框
    //m_stateLabel->setFrameStyle(QFrame::Box);
    //设置对齐方式:水平居中
    m_stateLabel->setAlignment(Qt::AlignHCenter);
    m_image1.load("./fly1.png");
    m_image2.load("./fly2.png");
    m_image3.load("./fly3.png");

    m_label1->setPixmap(QPixmap::fromImage(m_image1));
    m_label2->setPixmap(QPixmap::fromImage(m_image2));
    m_label3->setPixmap(QPixmap::fromImage(m_image3));

    QHBoxLayout* layout = new QHBoxLayout;
    layout->addWidget(m_label1);
    layout->addWidget(m_label2);
    layout->addWidget(m_label3);

    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addLayout(layout);
    mainLayout->addWidget(m_stateLabel);

    resize(m_image2.width() * 3,m_image2.height() * 2);
    //给m_label123安装事件过滤,指定(this)当前整个窗体监视我们的事件对象
    m_label1->installEventFilter(this);
    m_label2->installEventFilter(this);
    m_label3->installEventFilter(this);

}

EventFilter::~EventFilter()
{

}
//第一个参数是监视对象,第二个参数是事件
bool EventFilter::eventFilter(QObject *watched, QEvent *event)
{
    QMatrix matrix;
    QImage tmpImg;
    //放大图片
    matrix.scale(2.0,2.0);

    if(watched == m_label1)
    {
        //鼠标按下事件
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                m_stateLabel->setText(QStringLiteral("左键按下图片1"));
            }
            if(mouseEvent->buttons()&Qt::MidButton)
            {
                m_stateLabel->setText(QStringLiteral("中键按下图片1"));
            }
            if(mouseEvent->buttons()&Qt::RightButton)
            {
                m_stateLabel->setText(QStringLiteral("右键按下图片1"));
            }

            tmpImg = m_image1.transformed(matrix);
            m_label1->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type()==QEvent::MouseButtonRelease)
        {
            m_stateLabel->setText(QStringLiteral("鼠标释放图片1"));
            m_label1->setPixmap(QPixmap::fromImage(m_image1));
        }
    }
    else if(watched == m_label2)
    {
        //鼠标按下事件
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                m_stateLabel->setText(QStringLiteral("左键按下图片2"));
            }
            if(mouseEvent->buttons()&Qt::MidButton)
            {
                m_stateLabel->setText(QString("中键按下图片2"));
            }
            if(mouseEvent->buttons()&Qt::RightButton)
            {
                m_stateLabel->setText(QString("右键按下图片2"));
            }

            tmpImg = m_image2.transformed(matrix);
            m_label2->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type()==QEvent::MouseButtonRelease)
        {
            m_stateLabel->setText(QStringLiteral("鼠标释放图片2"));
            m_label2->setPixmap(QPixmap::fromImage(m_image2));
        }
    }
    else if(watched == m_label3)
    {
        //鼠标按下事件
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()&Qt::LeftButton)
            {
                m_stateLabel->setText(QStringLiteral("左键按下图片3"));
            }
            if(mouseEvent->buttons()&Qt::MidButton)
            {
                m_stateLabel->setText(QStringLiteral("中键按下图片3"));
            }
            if(mouseEvent->buttons()&Qt::RightButton)
            {
                m_stateLabel->setText(QStringLiteral("右键按下图片3"));
            }

            tmpImg = m_image3.transformed(matrix);
            m_label3->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type()==QEvent::MouseButtonRelease)
        {
            m_stateLabel->setText(QStringLiteral("鼠标释放图片3"));
            m_label3->setPixmap(QPixmap::fromImage(m_image3));
        }
    }

    //将事件交给上层对话框进行处理
    return QDialog::eventFilter(watched,event);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149

运行效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/573900
推荐阅读
相关标签