QT passed the order of events

table of Contents

 A, Qt offers five levels of event processing and filtering (after the first):

1, reimplement event function.

2, reimplement QObject :: event ().

3, install an event filter

4, install an event filter on the QApplication.

5, reimplement QApplication the notify () method.

Two, event and Even Filter

1. event () Introduction

2. event returns the value of the role

3. The event filter (Even Filter)


 A, Qt offers five levels of event processing and filtering (after the first):

1, reimplement event function.

比如: mousePressEvent(), keyPressEvent(),   paintEvent() 。

This is the most common event-handling method.


2, reimplement :: QObject Event () .

This is typically used when Qt does not provide a handler for that event. That is, when we add a new event.

 

3, install an event filter

installEventFilter()     --      bool eventFilter(QObject *target, QEvent *event)

 

4, installed on QApplication event filter .

The reason why this is listed separately because: the QApplication event filter will capture all application events , and get a *** the event . That event sent to any other event filter sent to QApplication before the event filter.

 

5, reimplement QApplication the notify () method.

Qt use notify () to distribute the event . To any event processor capture event capture before the event , the *** is to reimplement QApplication the notify () method.

Two, event and Even Filter

1. event () Introduction

event action function lies in the distribution of events, performed after even filter

bool myWidget::event(QEvent *e)
{
    if (e->type() == QEvent::KeyPress) 
    {
        //将QEvent对象转换为真正的QKeyEvent对象
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
        if (keyEvent->key() == Qt::Key_Tab) 
        {
            qDebug() << "You press tab.";
            return true;
        }
    }
    //按照原来的流程来进行事件的分发
    return QWidget::event(e);
}

2. event returns the value of the role

If the incoming event has been identified and processed, you need to return true, otherwise false. If the return value is true, then Qt would think that this event has been processed, will no longer send the event to other objects, but will continue with the next event in the event queue.

Returns the parent class when the event, i.e., return QWidget :: event (e); it also calls the parent component.

Qt system in dealing with the event, there is a mechanism called event propagation mechanism . In other words, sub-assemblies (for example, a QButton events), call the sub-components of the event after the function, will call the parent component (for example, QWidget ) the event function. event return value of the function it is to control such a process.

Note that, rewrite event after the function returns the best parent class event functions to handle other event distribution, otherwise it can only deal with their own definition of the event.

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

In this example, because the parent does not call QTextEdit the event function, we can only deal with cases of Tab, you press the other button you what reaction are gone. Similarly, events can not be spread.

3. The event filter (Even Filter)

virtual bool QObject::eventFilter ( QObject * watched, QEvent * event );

The return value is of type bool role with even function similar returns true as no longer forwarded, false is allowed to continue to be processed

class MainWindow : public QMainWindow
{
public:
    MainWindow();
protected:
    bool eventFilter(QObject *obj, QEvent *event);
private:
    QTextEdit *textEdit;
};
 
MainWindow::MainWindow()
{
    textEdit = new QTextEdit;
    setCentralWidget(textEdit);
    
    textEdit->installEventFilter(this);
}
 
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == textEdit) 
    {
        if (event->type() == QEvent::KeyPress) 
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            qDebug() << "you press" << keyEvent->key();
            //事件不再进行传播,拦截
            return true;
        } 
        else
        {
            return false;//继续传播
        }
    } 
    else 
    {
        //当不确定是否继续传播时,按照父类的方法来处理
        //即调用父类的evenFilter函数
        return QMainWindow::eventFilter(obj, event);

        //也可直接 return false; 不进行拦截, 且不继续传播

    }
}

 

Published 87 original articles · won praise 46 · views 80000 +

Guess you like

Origin blog.csdn.net/LearnLHC/article/details/100018552