一、创建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(); //忽略该事件
结果:多出现了"Widget键盘按下事件",并且lineedit无法输入了。
三、第三次修改
在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);
widget.cpp构造函数增加代码,并增加事件过滤器函数的定义:
-
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);
- }
五、最后的源码:
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();
- }
midget:
-
//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键盘按下事件");
- }