Qt事件学习
一、创建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键盘按下事件");
- }
Qt事件学习的更多相关文章
- PyQt(Python+Qt)学习随笔:使用实例方法赋值方式捕获事件
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 在<第15.17节 PyQt(Python+ ...
- 第15.17节 PyQt(Python+Qt)入门学习:PyQt图形界面应用程序的事件捕获方法大全及对比分析
老猿Python博文目录 老猿Python博客地址 按照老猿规划的章节安排,信号和槽之后应该介绍事件,但事件在前面的随笔<PyQt(Python+Qt)实现的GUI图形界面应用程序的事件捕获方法 ...
- PyQt学习随笔:Qt事件类QEvent详解
QEvent类是PyQt5.QtCore中定义的事件处理的基类,事件对象包含了事件对应的参数. <Python & PyQt学习随笔:PyQt主程序的基本框架>介绍了PyQt程序通 ...
- PyQt学习随笔:自定义Qt事件可以使用的事件类型的常量值范围
除了<PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料速查>介绍的Qt已经定义的事件外,Qt还支持自定义事件. 为了方便起见,可以使用 registerEventTyp ...
- PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料汇总详细内容速查
下表是Qt5.11提供的所有已经定义的事件类型常量及其含义说明(其中标蓝色的是老猿认为价值比较大的事件),事件的事件类型通过QEvent.type()来获取.由于老猿没有找到直接粘贴Excel表格的方 ...
- QT事件
qtevents多线程工作object存储 Another Look at Events(再谈Events) 最近在学习Qt事件处理的时候发现一篇很不错的文章,是2004年季刊的一篇文章,网上有这篇文 ...
- Qt入门学习——Qt 5 帮助文档的使用
Qt入门学习——Qt 5 帮助文档的使用 学习图形界面开发,肯定离不开帮助文档的使用,因为它不像 C 语言那样就那么几个函数接口,图形接口的接口可以用海量来形容,常用的我们可能能记住,其它的真的没有必 ...
- qt事件传递过程和处理
处理监控系统的时候遇到问题,在MainWidget中创建多个子Widget的时候,原意是想鼠标点击先让MainWidget截获处理后再分派给子Widget去处理,但调试后发现如果子Widget重新实现 ...
- QT入门学习笔记2:QT例程
转至:http://blog.51cto.com/9291927/2138876 Qt开发学习教程 一.Qt开发基础学习教程 本部分博客主要根据狄泰学院唐老师的<QT实验分析教程>创作,同 ...
随机推荐
- python3运行报错:TypeError: Object of type 'type' is not JSON serializable解决方法(详细)
返回结果先转成str 字符创
- 单词计数-MapReduceJob
pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3. ...
- ELK7.1.1之插件安装
在5.0版本之后不支持直接把插件包放入es安装目录的plugin目录下,需要单独安装:而且支持在线安装的插件很少,很多都是需要离线安装.以前的plugin变为elasticsearch-plugin ...
- git merge 上线操作流程
switch to branch git merge 主干到分支解决冲突,解决冲突文件,冲突文件,add to index 检查文件无<<<<<<< .=== ...
- python 简单抓取网页并写入excel实例
# -*- coding: UTF-8 -*- import requests from bs4 import BeautifulSoup import xlwt import time #获取第一页 ...
- react教程 — 性能优化
参考:https://segmentfault.com/a/1190000007811296?utm_medium=referral&utm_source=tuicool 或 https: ...
- jerry
jerry 题目描述 众所周知,Jerry 鼠是一只非常聪明的老鼠. Jerry 聪明到它可以计算64 位有符号整形数字的加减法. 现在,Jerry 写下了一个只由非负整数和加减号组成的算式.它想给这 ...
- [CSP-S模拟测试]:randomwalking(DP)
题目传送门(内部题59) 输入格式 第一行一个数$n$表示点数.第二行$n$个数$A_i$.接下来$n−1$行,每行两个数$u,v$表示$u$和$v$有边直接相连. 输出格式 一个数表示最小花费的起点 ...
- UITableView 支持左右滑动(二)
原理: 用tableView其中一个cell 来展示一个 UIScrollView, 在scrollview上很像放置子tableView 注意点: 外层tableView需要实现手势代理 /* 若重 ...
- JavaScript的日期对象
1.Date对象用来处理日期和时间. 2.创建Date对象的语法: var myDate = new Date(); 3.Date对象的常用方法: 格式:Date.XX(); getDate() 从 ...