Qt事件学习
2024-09-06 01:16:34
一、创建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键盘按下事件");
- }
最新文章
- 配置iis时,浏览项目提示 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
- python 中x%2 x&;1 判断偶数奇数 性能对比
- Windows下的UDP爆了10054--远程主机强迫关闭了一个现有的连接
- freemaker分页模板
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
- iOS9适配+warning消除
- php分页原理教程及简单实例
- 电脑本机ping通Linux虚拟机的方法
- [poj1644]放苹果
- django rest framework serializers
- js中 setTimeout延时0毫秒的作用
- Python minidom模块(DOM写入和解析XML)
- SQL Server统计数据库中表个数、视图个数、存储过程个数
- SQL 查询嵌套使用
- 20155319 2016-2017-2 《Java程序设计》第八周学习总结
- transform.forward和vector3.forward
- VS比较好用的扩展插件总结
- 【2017下集美大学软工1412班_助教博客】团队作业4——Alpha冲刺日志公示
- Hive学习之Locking
- iOS7中UIView的animateKeyframesWithDuration方法讲解
热门文章
- Angular JS - 3 - Angular JS 双向数据绑定
- SQL SERVER内部函数大全
- SGU 194 Reactor Cooling (无源上下界网络流)
- ThinkPHP5在PHP7以上使用QueryList4, ThinkCMF在PHP5中使用QueryList3教程
- [CSP-S模拟测试]:旅行(数学+线段树)
- python数据储存
- js练习题之图片背景轮播
- pycharm运行html文件报404错误
- springboot 应用程序的文件检索描述
- Maven 上传文件 Error creating bean with name &#39;multipartResolver&#39;: