事件传递流程图 Qt 5个级别事件处理和事件过滤方法。 下列代码分别在QObject中重新实现event。 给QObject安装事件过滤器。 重新实现notify函数。 重新实现事件处理器。 main.cpp
#include "widget.h" #include "myqapplication.h" #include <QApplication> int main(int argc, char *argv[]) { myQApplication a(argc, argv); //在QApplication中安装事件过滤器。 Widget w; w.show(); return a.exec(); }widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "myline.h" QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); public: bool event(QEvent *event); void keyPressEvent(QKeyEvent *event) ; protected: bool eventFilter(QObject *obj, QEvent *event); private: myLine *line; private: Ui::Widget *ui; }; #endif // WIDGET_Hwidget.cpp
#include "widget.h" #include "ui_widget.h" #include <QEvent> #include <QDebug> #include <QKeyEvent> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); line = new myLine(this); line->installEventFilter(this); } bool Widget::eventFilter(QObject *obj, QEvent *event) { if(obj == nullptr) return true; if(event->type() == QEvent::KeyPress) { qDebug()<<"widget eventfileter"; } return QWidget::eventFilter(obj, event); } bool Widget::event(QEvent *event) { if(event->type() == QEvent::KeyPress) { qDebug()<< "widget event"; } return QWidget::event(event); //调用父类的处理函数***************************** } void Widget::keyPressEvent(QKeyEvent *event) { qDebug()<<"widget keyPressEvent "; } Widget::~Widget() { delete ui; }myqapplication.cpp
#include "myqapplication.h" #include <QDebug> myQApplication::myQApplication(int &argc, char **argv): QApplication(argc, argv) { } bool myQApplication::notify(QObject *obj, QEvent *e) { if(e->type() == QEvent::KeyPress) { qDebug()<<"myQApplication KeyPress event"; } return QApplication::notify(obj, e); //调用父类的notify函数******************* }myqapplication.h
#ifndef MYQAPPLICATION_H #define MYQAPPLICATION_H #include <QApplication> class myQApplication : public QApplication { public: myQApplication(int &argc, char **argv); public: bool notify(QObject *, QEvent *); }; #endif // MYQAPPLICATION_Hmyline.cpp
#include "myline.h" #include <QEvent> #include <QDebug> #include <QKeyEvent> myLine::myLine(QWidget *parent): QLineEdit(parent) { } myLine::myLine(const QString &contents, QWidget *parent): QLineEdit(contents, parent) { } myLine::~myLine() { } bool myLine::event(QEvent *e) { if(e->type() == QEvent::KeyPress) { qDebug()<<"myLine event"; } return QLineEdit::event(e); //调用父类event函数****************** } void myLine::keyPressEvent(QKeyEvent *e) { qDebug()<<"myLine keyPressEvent"; QLineEdit::keyPressEvent(e); //调用父类事件处理函数************* }myLine.h
#ifndef MYLINE_H #define MYLINE_H #include <QLineEdit> class myLine : public QLineEdit { public: myLine(QWidget *parent = nullptr); myLine(const QString &contents, QWidget *parent = nullptr); ~myLine(); public: bool event(QEvent *) override; void keyPressEvent(QKeyEvent *) override; }; #endif // MYLINE_H注意事项:我们不管是使用事件过滤器,重新实现event,子类化QApplication重新实现notify,重写特殊事件处理函数都是一样的,在我们无法处理的事件一定要调用父类事件响应的处理函数。在代码中已经标注出需要这样处理的位置。