一、创建Qt gui应用对应的源码:
点击(此处)折叠或打开
- //mylineedit.h
- #ifndef MYLINEEDIT_H
- #define MYLINEEDIT_H
-
- #include <QWidget>
- #include <QLineEdit>
-
- class MyLineEdit : public QLineEdit
- {
- public:
- explicit MyLineEdit(QWidget *parent = 0);
- protected:
- void keyPressEvent(QKeyEvent *event);
- };
-
- #endif // MYLINEEDIT_H
-
-
- //mylineedit.cpp
- #include "mylineedit.h"
- #include <QLineEdit>
- #include <QDebug>
- #include <QKeyEvent>
-
- MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
- {
-
- }
-
- void MyLineEdit::keyPressEvent(QKeyEvent *event)
- {
- qDebug() << QObject::tr("MyLineEdit键盘按下事件");
- }
此时只会出现“MyLineEidt键盘按下事件”。
二、第二次修改
在mylineedit.cpp中keyPressEvent()添加- event->ignore(); //忽略该事件
三、第三次修改
在mylineedit.h中添加public函数:- bool event(QEvent *event);
- bool MyLineEdit::event(QEvent *event)
- {
- if(event->type() == QEvent::KeyPress)
- qDebug()<<QObject::tr("MylineEdit的event()函数");
- return QLineEdit::event(event);
- }
四、第四次修改
widget.h中public申明:- bool eventFilter(QObject *obj,QEvent *event);
- lineEdit->installEventFilter(this); //在widget上为lineEdit安装事件过滤器
-
- bool Widget::eventFilter(QObject *watched, QEvent *event)
- {
- if(watched==lineEdit) {
- if(event->type()==QEvent::KeyPress)
- qDebug()<<QObject::tr("Widget的事件过滤器");
- }
- return QWidget::eventFilter(watched, event);
- }
![](/attachment/201708/6/29512885_1502008463Zw77.png)
五、最后的源码:
mylineedit:
- //mylineedit.h
- #ifndef MYLINEEDIT_H
- #define MYLINEEDIT_H
-
- #include <QWidget>
- #include <QLineEdit>
-
- class MyLineEdit : public QLineEdit
- {
- public:
- explicit MyLineEdit(QWidget *parent = 0);
- bool event(QEvent *event);
-
- protected:
- void keyPressEvent(QKeyEvent *event);
- };
-
- #endif // MYLINEEDIT_H
-
-
- //mylineedit.cpp
- #include "mylineedit.h"
- #include <QLineEdit>
- #include <QDebug>
- #include <QKeyEvent>
-
- MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
- {
-
- }
-
- bool MyLineEdit::event(QEvent *event)
- {
- if(event->type() == QEvent::KeyPress)
- qDebug()<<QObject::tr("MylineEdit的event()函数");
- return QLineEdit::event(event);
- }
-
- void MyLineEdit::keyPressEvent(QKeyEvent *event)
- {
- qDebug() << QObject::tr("MyLineEdit键盘按下事件");
- QLineEdit::keyPressEvent(event);
- event->ignore();
- }
- //wdiget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
-
- class MyLineEdit;
-
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
- bool eventFilter(QObject *watched, QEvent *event);
- protected:
- void keyPressEvent(QKeyEvent *event);
- private:
- Ui::Widget *ui;
- MyLineEdit *lineEdit;
- };
-
- #endif // WIDGET_H
-
-
- //widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include "mylineedit.h"
- #include <QKeyEvent>
- #include <QDebug>
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- lineEdit = new MyLineEdit(this);
- lineEdit->move(100, 100);
- lineEdit->installEventFilter(this); //在widget上为lineEdit安装事件过滤器
- ui->setupUi(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- bool Widget::eventFilter(QObject *watched, QEvent *event)
- {
- if(watched==lineEdit) {
- if(event->type()==QEvent::KeyPress)
- qDebug()<<QObject::tr("Widget的事件过滤器");
- }
- return QWidget::eventFilter(watched, event);
- }
-
- void Widget::keyPressEvent(QKeyEvent *event)
- {
- qDebug()<<QObject::tr("Widget键盘按下事件");
- }
给主人留下些什么吧!~~
评论热议