2020-08-14

QT Event机制测试程序
代码:https://github.com/pengrui2009/QEventTest
2020-08-14_第1张图片
这里我们测试QKeyPress时间:
在主窗口放置一个QLineEdit控件,并提升为MyLineEdit.
新建MyLineEdit类:

#include 
#include 
#include 
#include "mylineedit.h"

//MyLineEdit::MyLineEdit()
//{

//}

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

}

bool MyLineEdit::event(QEvent* e)
{
   if( e->type() == QEvent::KeyPress )
   {
       qDebug() << "MyLineEdit::event";
   }
   return QLineEdit::event(e);//调用默认事件处理函数
}

void MyLineEdit::keyPressEvent(QKeyEvent* e)//键盘按键事件
{
    qDebug() << "MyLineEdit::keyPressEvent";
    QLineEdit::keyPressEvent(e);
    e->ignore();//当前对象忽略处理此事件,所以父组件对象进行事件处理函数的调用
}

#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H

#include 
#include 

class MyLineEdit : public QLineEdit
{
public:
    MyLineEdit(QWidget *parent = 0);
protected:
    bool event(QEvent* e);
    void keyPressEvent(QKeyEvent* e);
};

#endif // MYLINEEDIT_H

修改mianwindow.cpp:

#include 
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->lineEdit->installEventFilter(this);
}

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

bool MainWindow::event(QEvent *e)
{
    if (e->type() == QEvent::KeyPress)
    {
        qDebug() << "MainWindow event";
        //将QEvent对象转换为真正的QKeyEvent对象
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
        if (keyEvent->key() == Qt::Key_Tab)
        {
            qDebug() << "You press tab.";
            return true;
        }

    }
    //按照原来的流程来进行事件的分发
    return QMainWindow::event(e);
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    qDebug() << "MainWindow mousePressEvent";
}

void MainWindow::keyPressEvent(QKeyEvent* e)
{
    qDebug() << "MainWindow::keyPressEvent";
    QMainWindow::keyPressEvent(e);
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    bool result = true;

    if (watched == ui->lineEdit)
    {
        if (event->type() == QEvent::KeyPress)
        {
            qDebug() << "MainWindow eventFilter";
            return false;
        }
    }

    return false;
}

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    bool event(QEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void keyPressEvent(QKeyEvent* e);
    bool eventFilter(QObject *, QEvent *);
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

运行,我们可以根据后台打印信息发现,当我们在QLineEdit控件输入字符时,先经过MainWindow中EventFilter处理,然后经过QLineEdit控件处理,最后回到MainWindow中处理.

2020-08-14_第2张图片
处理流程:如果 installEventFilter,先执行主函数中的eventFilter函,如果主函数eventFilter返回false,则继续执行QLineEdit的event函数,然后在执行QLineEdit控件keyPressEvent,最后执行MainWindow中的event函数和keyPressEvent函数.
1.eventFilter返回false,则继续处理
2.event返回true,则继续处理
3.keyPressEvent为最后的分发处理函数,代表事件的最终处理。

你可能感兴趣的:(QT自定义控件)