您的位置:首页 > 编程语言 > Qt开发

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语言指针详解-完整版...

给主人留下些什么吧!~~

评论热议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: