Qt事件学习
2017-08-15 09:12
281 查看
一、创建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键盘按下事件");
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2"
a75b
,"bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(0) | 评论(0) | 转发(0) |
0
上一篇:PIC16F877A的TIME0学习
下一篇:Qt对话框部分学习
相关热门文章
test123
编写安全代码——小心有符号数...
使用openssl api进行加密解密...
一段自己打印自己的c程序...
彻底搞定C语言指针详解-完整版...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- QT学习笔记13消息机制和事件
- Qt学习之路之鼠标事件
- QT 鼠标和滚轮事件学习
- Qt 的线程与事件循环——可打印threadid进行观察槽函数到底是在哪个线程里执行,学习moveToThread的使用)
- Qt事件学习1
- QT学习之事件处理
- Qt 学习(8)Qt Creator中鼠标键盘事件的处理实现自定义鼠标指针(转)
- [Q学习]10 Qt事件系统——处理事件
- qt学习之键盘事件( keyPressEvent)
- (转载)Qt 的线程与事件循环——可打印threadid进行观察槽函数到底是在哪个线程里执行,学习moveToThread的使用)
- QT5 学习之路08---事件的接受与忽略
- [Q学习]11 Qt事件系统——过滤事件
- QT学习 之 事件与事件过滤器(分为五个层次)
- Qt学习: 给程序添加快捷键事件的几种方法介绍
- Qt学习: 鼠标事件总结(转)
- QT中窗口刷新事件的学习总结
- qt练习11 鼠标,按键,滚轮事件学习
- Qt学习之路之鼠标事件
- QT开始学习之事件理解
- qt事件相关学习笔记