Chinaunix首页 | 论坛 | 博客
  • 博客访问: 498605
  • 博文数量: 223
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2145
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-01 10:23
个人简介

该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: C/C++

2017-08-06 16:36:49

一、创建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. }









阅读(1018) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~