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

Qt事件学习的更多相关文章

  1. PyQt(Python+Qt)学习随笔:使用实例方法赋值方式捕获事件

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 在<第15.17节 PyQt(Python+ ...

  2. 第15.17节 PyQt(Python+Qt)入门学习:PyQt图形界面应用程序的事件捕获方法大全及对比分析

    老猿Python博文目录 老猿Python博客地址 按照老猿规划的章节安排,信号和槽之后应该介绍事件,但事件在前面的随笔<PyQt(Python+Qt)实现的GUI图形界面应用程序的事件捕获方法 ...

  3. PyQt学习随笔:Qt事件类QEvent详解

    QEvent类是PyQt5.QtCore中定义的事件处理的基类,事件对象包含了事件对应的参数. <Python & PyQt学习随笔:PyQt主程序的基本框架>介绍了PyQt程序通 ...

  4. PyQt学习随笔:自定义Qt事件可以使用的事件类型的常量值范围

    除了<PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料速查>介绍的Qt已经定义的事件外,Qt还支持自定义事件. 为了方便起见,可以使用 registerEventTyp ...

  5. PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料汇总详细内容速查

    下表是Qt5.11提供的所有已经定义的事件类型常量及其含义说明(其中标蓝色的是老猿认为价值比较大的事件),事件的事件类型通过QEvent.type()来获取.由于老猿没有找到直接粘贴Excel表格的方 ...

  6. QT事件

    qtevents多线程工作object存储 Another Look at Events(再谈Events) 最近在学习Qt事件处理的时候发现一篇很不错的文章,是2004年季刊的一篇文章,网上有这篇文 ...

  7. Qt入门学习——Qt 5 帮助文档的使用

    Qt入门学习——Qt 5 帮助文档的使用 学习图形界面开发,肯定离不开帮助文档的使用,因为它不像 C 语言那样就那么几个函数接口,图形接口的接口可以用海量来形容,常用的我们可能能记住,其它的真的没有必 ...

  8. qt事件传递过程和处理

    处理监控系统的时候遇到问题,在MainWidget中创建多个子Widget的时候,原意是想鼠标点击先让MainWidget截获处理后再分派给子Widget去处理,但调试后发现如果子Widget重新实现 ...

  9. QT入门学习笔记2:QT例程

    转至:http://blog.51cto.com/9291927/2138876 Qt开发学习教程 一.Qt开发基础学习教程 本部分博客主要根据狄泰学院唐老师的<QT实验分析教程>创作,同 ...

随机推荐

  1. ECS运维:操作系统有异常?诊断日志来帮忙!

    ​云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新.阿里云使用严格的IDC标准.服务器准入标准 ...

  2. PHP curl_copy_handle函数

    curl_copy_handle — 复制一个cURL句柄和它的所有选项 说明 resource curl_copy_handle ( resource $ch ) 复制一个cURL句柄并保持相同的选 ...

  3. linux网络接口,struct ifreq struct ifconf结构

    网络相关的ioctl请求的request参数及arg地址必须指向的数据类型如下表所示: 接口 SIOCGIFCONF SIOCSIFADDR SIOCGIFADDR SIOCSIFBRDADDR SI ...

  4. 【LeetCode 96】不同的二叉搜索树

    题目链接 [题解] 我们可以枚举这棵树的根节点在i处. 现在问题就变成. 1..i-1这i-1个节点组成的树和i+1..n这n-i个节点组成的树的个数的问题了. 假设他们俩的结果分别是cnt1和cnt ...

  5. EF框架实增删改查

    数据库链接配置: <connectionStrings> <add name="XxzxWorkEntities" connectionString=" ...

  6. 转载:VScode 好用插件集合

    --------------------- 作者:JayveeWong 来源:CSDN 原文:https://blog.csdn.net/weixin_42776111/article/details ...

  7. Apache + Tomcat 集群的配置

    公司最近新接了一个项目,客户要求WEB SERVER 必须是APACHE,,由于之前的项目一直都是NGINX,,无奈啊,,由于对方的IT又是在三哥的过度,经过一番的争执只能顺应三哥三姐的要求..

  8. Asp.Net Core 第01局:项目创建和部署 转载https://www.jianshu.com/p/9c9750e23b3e

    总目录 一.前言 本文通过从项目创建到部署,简单介绍Asp.Net Core. 二.环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 三.开局 第一手:创建项目   ...

  9. 未找到源文件:C:\loadrunner-11\urunner MSI\bin\icudt36.dll.o1d解决方法

    安装HP LoadRunner 11.00 未找到源文件:C:\loadrunner-11\urunner MSI\bin\icudt36.dll.o1d 下载loadrunner11 使用迅雷下载, ...

  10. angularJS 入门知识

    模块:模块可以定义自己的控制器.服务.工厂类以及指令 模块可以依赖其他模块 模块两大常见错误: 定义模块的时候忘记第二个参数,变成使用模块而不是定义模块 使用模块的时候忘记引用依赖模块