QT中的event事件解析

  在QT中使用一个对象表示一个事件,继承自QEvent类。需要说明的是事件与信号是不相同的。在每个程序的main()函数的最开始都会调用QApplication类的exec()函数,它会使Qt应用程序进入事件循环,这样就可以使应用程序在运行时候接受发生的各种事件,一旦有事件发生,Qt便会创建一个相应的QEvent子类对象的事件来表示,然后传递给相应的QObject对象或其子对象。


下面我们创建一个Qt Gui工程,基类选择QWidget,还是直接上代码,个别地方我会注释讲解的。


mian.cpp   

#include <QtGui/QApplication>
#include "widget.h"
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    Widget w;
    w.show();
    
    return a.exec();
}

mylineedit.h

#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H

#include <QLineEdit>

class MyLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    explicit MyLineEdit(QWidget *parent = 0);
    bool event(QEvent *event);

protected:
    void keyPressEvent(QKeyEvent *event);


signals:

public slots:

};

#endif // MYLINEEDIT_H

在这个文件中我们声明了一个 keyPressEvent按键事件处理函数,这个函数在QlineEdit类中已经被声明了,只是是一个虚函数,所以这里我们可以重新定义。


myLineEdit.cpp

#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>

MyLineEdit::MyLineEdit(QWidget *parent) :
    QLineEdit(parent)
{

}

bool MyLineEdit::event(QEvent *event)
{
    if(event->type()==QEvent::KeyPress)
        qDebug() << tr("MyLineEdit event functions");
    return QLineEdit::event(event);   /* exec QLineEdit default event() funcion dealwith  */
}


void MyLineEdit::keyPressEvent(QKeyEvent *event)
{
    qDebug() << tr("MyLineEdit键盘按下事件");

    QLineEdit::keyPressEvent(event);    /* exec QLineEdit default event dealwith  */

    event->ignore();       /* ignore the event */
}

myLineEdit.cpp中构造函数没有做任何操作,MyLineEdit::keyPressEvent函数就是我们定义的按键事件函数,当按下按键后就会调用该函数,QLineEdit::keyPressEvent(event);函数意思是执行默认操作,因为该事件调用了这个函数,而本身事件就被忽略了,event->ignore();就是忽略这个事件,然后在widget窗口如果定义了这个keyPressEvent,也就可以触发这个事件。 MyLineEdit::event是一个大类触发事件,按键事件只是它的一个子类。


widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class MyLineEdit;

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
        bool eventFilter(QObject *obj, QEvent *event);

protected:
    void keyPressEvent(QKeyEvent *event);

private:
    Ui::Widget *ui;
    MyLineEdit *lineEdit;
};

#endif // WIDGET_H

widget类中声明了一个事件过滤函数eventFilter,有这个函数后,我们可以筛选我们想要的事件。创建了一个指向MyLineEdit类型的一个lineEdit指针。


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)
{
    ui->setupUi(this);
    lineEdit =new MyLineEdit(this);
    lineEdit->move(100,100);
    lineEdit->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *obj, QEvent *event)
{
    if(obj==lineEdit)
    {
        if(event->type()==QEvent::KeyPress)
            qDebug() << tr("Widget event Filter");
    }
    return QWidget::eventFilter(obj,event);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    qDebug() << tr("Widget键盘按下事件");
}


设计界面和事件的打印顺序如下:





 

                                                                                                                         文章出自:Linux_Google

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Qt 事件循环机制是 Qt 框架的核心,它负责处理所有的事件和信号槽机制。Qt 事件循环机制的源码实现比较复杂,涉及到多个类和文件。下面是一个简单的 Qt 事件循环机制源码解析: 1. 事件循环机制的入口函数是 QCoreApplication::exec(),它是一个静态函数,会启动事件循环。 2. 在事件循环Qt 会不断地从事件队列取出事件,然后分发给对应的对象处理。事件队列是一个双向链表,由 QEventDispatcherWin32(Windows 平台)或 QEventDispatcherUNIX(Unix/Linux 平台)管理。 3. 事件分发的过程是通过 QObject::event() 函数实现的。当一个事件分发给一个对象时,它会首先调用对象的 event() 函数,如果该函数返回 false,那么事件会被传递给对象的父对象,直到有一个对象的 event() 函数返回 true,或者事件传递到了根对象。 4. 信号槽机制的实现也依赖于事件循环机制。当一个信号被发射时,它会被转化为一个事件,然后被插入到事件队列。当事件循环处理到该事件时,它会调用对应的槽函数。 5. 事件循环还涉及到消息循环和定时器机制。消息循环通过 QSocketNotifier 和 QAbstractEventDispatcher 类实现,它可以监听文件描述符和套接字等事件。定时器机制通过 QTimer 类实现,它可以定时触发事件。 总之,Qt 事件循环机制的源码实现非常复杂,但它是 Qt 框架的核心,负责处理所有的事件和信号槽机制,是 Qt 能够高效运行的关键。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值