一、创建Qt gui应用对应的源码:

点击(此处)折叠或打开

  1. //mylineedit.h
  2. #ifndef MYLINEEDIT_H
  3. #define MYLINEEDIT_H
  4. #include <QWidget>
  5. #include <QLineEdit>
  6. class MyLineEdit : public QLineEdit
  7. {
  8. public:
  9. explicit MyLineEdit(QWidget *parent = 0);
  10. protected:
  11. void keyPressEvent(QKeyEvent *event);
  12. };
  13. #endif // MYLINEEDIT_H
  14. //mylineedit.cpp
  15. #include "mylineedit.h"
  16. #include <QLineEdit>
  17. #include <QDebug>
  18. #include <QKeyEvent>
  19. MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
  20. {
  21. }
  22. void MyLineEdit::keyPressEvent(QKeyEvent *event)
  23. {
  24. qDebug() << QObject::tr("MyLineEdit键盘按下事件");
  25. }

此时只会出现“MyLineEidt键盘按下事件”。

二、第二次修改

在mylineedit.cpp中keyPressEvent()添加

  1. event->ignore(); //忽略该事件

结果:多出现了"Widget键盘按下事件",并且lineedit无法输入了。

三、第三次修改

在mylineedit.h中添加public函数:

  1. bool event(QEvent *event);

然后定义:

  1. bool MyLineEdit::event(QEvent *event)
  2. {
  3. if(event->type() == QEvent::KeyPress)
  4. qDebug()<<QObject::tr("MylineEdit的event()函数");
  5. return QLineEdit::event(event);
  6. }

四、第四次修改

widget.h中public申明:

  1. bool eventFilter(QObject *obj,QEvent *event);

widget.cpp构造函数增加代码,并增加事件过滤器函数的定义:

  1. lineEdit->installEventFilter(this); //在widget上为lineEdit安装事件过滤器
  2. bool Widget::eventFilter(QObject *watched, QEvent *event)
  3. {
  4. if(watched==lineEdit) {
  5. if(event->type()==QEvent::KeyPress)
  6. qDebug()<<QObject::tr("Widget的事件过滤器");
  7. }
  8. return QWidget::eventFilter(watched, event);
  9. }


五、最后的源码:
mylineedit:

  1. //mylineedit.h
  2. #ifndef MYLINEEDIT_H
  3. #define MYLINEEDIT_H
  4. #include <QWidget>
  5. #include <QLineEdit>
  6. class MyLineEdit : public QLineEdit
  7. {
  8. public:
  9. explicit MyLineEdit(QWidget *parent = 0);
  10. bool event(QEvent *event);
  11. protected:
  12. void keyPressEvent(QKeyEvent *event);
  13. };
  14. #endif // MYLINEEDIT_H
  15. //mylineedit.cpp
  16. #include "mylineedit.h"
  17. #include <QLineEdit>
  18. #include <QDebug>
  19. #include <QKeyEvent>
  20. MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
  21. {
  22. }
  23. bool MyLineEdit::event(QEvent *event)
  24. {
  25. if(event->type() == QEvent::KeyPress)
  26. qDebug()<<QObject::tr("MylineEdit的event()函数");
  27. return QLineEdit::event(event);
  28. }
  29. void MyLineEdit::keyPressEvent(QKeyEvent *event)
  30. {
  31. qDebug() << QObject::tr("MyLineEdit键盘按下事件");
  32. QLineEdit::keyPressEvent(event);
  33. event->ignore();
  34. }

midget:

  1. //wdiget.h
  2. #ifndef WIDGET_H
  3. #define WIDGET_H
  4. #include <QWidget>
  5. class MyLineEdit;
  6. namespace Ui {
  7. class Widget;
  8. }
  9. class Widget : public QWidget
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit Widget(QWidget *parent = 0);
  14. ~Widget();
  15. bool eventFilter(QObject *watched, QEvent *event);
  16. protected:
  17. void keyPressEvent(QKeyEvent *event);
  18. private:
  19. Ui::Widget *ui;
  20. MyLineEdit *lineEdit;
  21. };
  22. #endif // WIDGET_H
  23. //widget.cpp
  24. #include "widget.h"
  25. #include "ui_widget.h"
  26. #include "mylineedit.h"
  27. #include <QKeyEvent>
  28. #include <QDebug>
  29. Widget::Widget(QWidget *parent) :
  30. QWidget(parent),
  31. ui(new Ui::Widget)
  32. {
  33. lineEdit = new MyLineEdit(this);
  34. lineEdit->move(100, 100);
  35. lineEdit->installEventFilter(this); //在widget上为lineEdit安装事件过滤器
  36. ui->setupUi(this);
  37. }
  38. Widget::~Widget()
  39. {
  40. delete ui;
  41. }
  42. bool Widget::eventFilter(QObject *watched, QEvent *event)
  43. {
  44. if(watched==lineEdit) {
  45. if(event->type()==QEvent::KeyPress)
  46. qDebug()<<QObject::tr("Widget的事件过滤器");
  47. }
  48. return QWidget::eventFilter(watched, event);
  49. }
  50. void Widget::keyPressEvent(QKeyEvent *event)
  51. {
  52. qDebug()<<QObject::tr("Widget键盘按下事件");
  53. }

最新文章

  1. 配置iis时,浏览项目提示 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
  2. python 中x%2 x&amp;1 判断偶数奇数 性能对比
  3. Windows下的UDP爆了10054--远程主机强迫关闭了一个现有的连接
  4. freemaker分页模板
  5. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
  6. iOS9适配+warning消除
  7. php分页原理教程及简单实例
  8. 电脑本机ping通Linux虚拟机的方法
  9. [poj1644]放苹果
  10. django rest framework serializers
  11. js中 setTimeout延时0毫秒的作用
  12. Python minidom模块(DOM写入和解析XML)
  13. SQL Server统计数据库中表个数、视图个数、存储过程个数
  14. SQL 查询嵌套使用
  15. 20155319 2016-2017-2 《Java程序设计》第八周学习总结
  16. transform.forward和vector3.forward
  17. VS比较好用的扩展插件总结
  18. 【2017下集美大学软工1412班_助教博客】团队作业4——Alpha冲刺日志公示
  19. Hive学习之Locking
  20. iOS7中UIView的animateKeyframesWithDuration方法讲解

热门文章

  1. Angular JS - 3 - Angular JS 双向数据绑定
  2. SQL SERVER内部函数大全
  3. SGU 194 Reactor Cooling (无源上下界网络流)
  4. ThinkPHP5在PHP7以上使用QueryList4, ThinkCMF在PHP5中使用QueryList3教程
  5. [CSP-S模拟测试]:旅行(数学+线段树)
  6. python数据储存
  7. js练习题之图片背景轮播
  8. pycharm运行html文件报404错误
  9. springboot 应用程序的文件检索描述
  10. Maven 上传文件 Error creating bean with name &#39;multipartResolver&#39;: