Qt中的事件处理(二)

1.事件被组件对象处理后可能传递到其父组件对象上

2.QEvent中的关键成员函数

  • void ignore():接收者忽略当前事件,事件可能传递给父组件
  • void accept():接收者期望处理当前事件
  • bool isAccepted():判断当前事件是否被处理

Widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "MyLineEdit.h"

class Widget : public QWidget
{
    Q_OBJECT
    MyLineEdit myLineEdit;

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget() override;
    bool event(QEvent *);
    void keyPressEvent(QKeyEvent* e);

};
#endif // WIDGET_H

Widget.cpp

复制代码
#include "Widget.h"
#include <QDebug>
#include <QEvent>


Widget::Widget(QWidget *parent)
    : QWidget(parent), myLineEdit(this)
{

}

bool Widget::event(QEvent *e)
{
    if( e->type() == QEvent::KeyPress )
    {
        qDebug() << "Widget::event";
    }
    return QWidget::event(e);
}

void Widget::keyPressEvent(QKeyEvent* e)
{
    qDebug() << "Widget::keyPressEvent";
    QWidget::keyPressEvent(e);
}
Widget::~Widget()
{

}

MyLineEdit.h

复制代码
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H

#include <QLineEdit>
#include <QWidget>

class MyLineEdit : public QLineEdit
{
public:
    MyLineEdit(QWidget* parent = 0);
    bool event(QEvent *);
    void keyPressEvent(QKeyEvent* e);


};

#endif // MYLINEEDIT_H

MyLineEdit.cpp

复制代码
#include "MyLineEdit.h"
#include <QDebug>
#include <QEvent>
#include <QKeyEvent>

MyLineEdit::MyLineEdit(QWidget* parent) : QLineEdit(parent)
{

}

bool MyLineEdit::event(QEvent* e)
{
    if( e->type() == QEvent::KeyPress )
    {
        qDebug() << "MyLineEdit::event";
    }
    return QLineEdit::event(e);
}
void MyLineEdit::keyPressEvent(QKeyEvent* e)
{
    qDebug() << "MyLineEdit::keyPressEvent";
    QLineEdit::keyPressEvent(e);
    e->ignore();
}

main.cpp

复制代码
#include "Widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return QCoreApplication::exec();
}

运行结果:键盘按下后

3.Qt中的事件过滤器

  • 事件过滤器可以对其他组件接收到的事件进行监控
  • 任意的QObject对象都可以作为事件过滤器使用
  • 事件过滤器对象需要重写eventFilter()函数

组件通过installEventFilter()函数安装事件过滤器

  • 事件过滤器在组件之前接收到事件
  • 事件过滤器能够决定是否将事件转发到组件对象

在Widget.h中添加成员函数

复制代码
bool eventFilter(QObject* obj, QEvent* e); //第一个参数表示被监控的对象,第二个参数表示需要过滤的事件

Widget.cpp

复制代码
Widget::Widget(QWidget *parent)
    : QWidget(parent), myLineEdit(this)
{
    myLineEdit.installEventFilter(this);
}

bool Widget::eventFilter(QObject* obj, QEvent* e)
{
    bool ret = true;
    if( (obj == &myLineEdit) && (e->type() == QEvent::KeyPress) )
    {
        qDebug() << "Widget::eventFilter";
        QKeyEvent* evt = dynamic_cast<QKeyEvent*>(e);
        switch (evt->key()) {
        case Qt::Key_0:
        case Qt::Key_1:
        case Qt::Key_2:
        case Qt::Key_3:
        case Qt::Key_4:
        case Qt::Key_5:
        case Qt::Key_6:
        case Qt::Key_7:
        case Qt::Key_8:
        case Qt::Key_9:
            ret = false;
            break;
        default:
            break;
        }
    }
    else
    {
        ret = QWidget::eventFilter(obj, e);
    }
    return ret;
}

运行结果:

当键盘按下数字时:

按下其他按钮时

相关推荐
艾莉丝努力练剑5 分钟前
【AI大模型接入SDK】C++ ChatSDK使用手册
开发语言·网络·c++·人工智能·学习·大模型
“AI国潮设计-小江”9 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞翻糖吐司”构图,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
程序喵大人9 小时前
【C++入门】编译链接模型 - 01 一个 C++ 程序是怎样变成可执行文件的
开发语言·c++·编译链接模型
sunshine22 girl10 小时前
Java学习七 Java 项目实战1-项目创建和主页面搭建
java·开发语言·学习
菜鸟~noob23311 小时前
【电子战】第三季预告—波武器与电子系统强力毁伤
开发语言·数据库·matlab·电子战
惊讶的猫12 小时前
工具选对之后还不够:商城 Agent 的参数与权限治理
开发语言·python
cmpxr_12 小时前
【MicroPython】如何刷新ESP8266的固件
开发语言·micropython
搭贝12 小时前
上厕所的时间搭好一套系统:AI自动生成实测
开发语言·人工智能·低代码·ai·php·搭贝
智感子13 小时前
智能称重·数据上云:从仪表读数到可追溯的计量凭证
开发语言·php
\光辉岁月/13 小时前
7.javase-面向对象
java·开发语言