Events in Qt (1)


1. Introduction to Qt events

Qt provides many events for interacting with external devices, such as mouse event QMouseEvent, keyboard event QKeyEvent, etc.

The distribution of events in Qt is roughly as follows:
1. QCoreApplication::notify function. Events in Qt are instances of QEvent-derived classes, and all QObject-derived classes can handle events. The starting point of events in the Qt framework is the QCoreApplication::notify function.
2. Apply the filter.
3. Object filter.
4. QObject::event() function.
5. Specific event handling functions, such as keyPressEvent(), etc.

2. Rewrite specific event handlers

Qt can add the power it wants for specific events.

Definition in .h file:

protected:
    virtual void mousePressEvent(QMouseEvent *event);

Definition in .cpp file:

void Widget::mousePressEvent(QMouseEvent *event)
{
    int xPt = event->pos().x();
    int yPt = event->pos().y();
    qDebug() << "QWidget " << xPt << ", " << yPt;
    return QWidget::mousePressEvent(event);
}

3. Event delivery and processing

An event that can be accepted (calling the QEvent::accept() method to accept an event) or ignored (calling the QEvent::ignore() method to ignore an event). Accepted events will not be delivered to other objects; ignored events will be delivered to other objects that may care about the event until an object handles it or all objects say they don't care about it.

Here is an example of event delivery:
.h example:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>

class PushButton : public QPushButton
{
    Q_OBJECT

public:
    PushButton(QWidget *parent = nullptr);
    ~PushButton();

protected:
    virtual void mousePressEvent(QMouseEvent *event);
};

// -----------------------------------------------------------------------------------
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

protected:
    virtual void mousePressEvent(QMouseEvent *event);

private:
    QPushButton *m_Button = nullptr;
};

#endif // WIDGET_H

.cpp example:

#include "widget.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QMouseEvent>

PushButton::PushButton(QWidget *parent)
    :QPushButton(parent)
{
    this->setFixedSize(200, 200);
}

PushButton::~PushButton()
{

}

void PushButton::mousePressEvent(QMouseEvent *event)
{
    QPushButton::mousePressEvent(event);
    qDebug() << "MousePressed " << event->pos().x() << ", " << event->pos().y();
    event->ignore();
}

// -----------------------------------------------------------------------------------
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    m_Button = new PushButton(this);
    m_Button->setText("OK");

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(m_Button);

    QObject::connect(m_Button, &QPushButton::clicked, [=](void)->void{
       qDebug() << "PushButton Pressed!" ;
    });

    this->setGeometry(100, 100, 800, 600);
}

Widget::~Widget()
{

}

void Widget::mousePressEvent(QMouseEvent *event)
{
    int xPt = event->pos().x();
    int yPt = event->pos().y();
    qDebug() << "QWidget " << xPt << ", " << yPt;
    return QWidget::mousePressEvent(event);
}

The running result after clicking the button,
MousePressed 144 , 77
QWidget 157 , 277
PushButton Pressed!

QPushButton::mousePressEvent(event) will receive mouse events by default. After the button processes the MousePress event, the event->ignore() function is called and the event is ignored and passed down to the Widget class, and the Widget class handles the mousePress event.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325588532&siteId=291194637