(十三)事件分发器——event()函数,事件过滤

事件分发器——event()函数

事件过滤

事件进入窗口之前被拦截 eventFilter 

#include "mywidget.h"
#include "ui_mywidget.h"
#include <QDebug>

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    // 给MyLabel 安装事件过滤器
    // 参数 谁来过滤label 的事件
    ui->mylabel->installEventFilter(this);
    ui->label_2->installEventFilter(this);
}

MyWidget::~MyWidget()
{
    delete ui;
}

void MyWidget::mousePressEvent(QMouseEvent *)
{
    qDebug() << "+++++++++++++";
}

bool MyWidget::eventFilter(QObject *obj, QEvent *e)
{
    // 判断对象
    if (obj == ui->mylabel) {
        // 过滤事件
        if (e->type() == QEvent::MouseMove)
        {
            ui->mylabel->setText("++++++++++");
            return true;
        }
    }
    if (obj == ui->label_2) {
        if (e->type() == QEvent::MouseMove)
        {
            ui->label_2->setText("**********");
            return true;
        }
    }
    // 执行默认处理
    return QWidget::eventFilter(obj,e);
}
mywidget.cpp

相关文章:

  • 2022-12-23
  • 2021-11-11
  • 2021-11-17
  • 2021-10-09
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
猜你喜欢
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2021-08-15
  • 2022-02-08
  • 2022-12-23
相关资源
相似解决方案