2. Mouse event monitoring of Qt Project

Mouse event monitoring:

For the related operations of mouse time monitoring, we need to select the QWidget base class when we resume the project, and do not select the QMainWindow base class, as shown below:

Base class:QWidget

Step1: We first define the UI design of the entire Qt software interface:

According to the design, Qt Creator automatically generates the following HTML script for us:

View Code

The effect of the actual interface is as follows:

Step2: According to the actual situation of the design, we carry out the relevant trigger mechanism for the Button on each interface:

a ) We need to include the Mouse related header files in

#include <QMouseEvent>

b) The related functions of mouse events are encapsulated in the Widget base class. For the above three types of events, we will list them as follows:

void Widget::mousePressEvent(QMouseEvent *e)
void Widget::mouseMoveEvent(QMouseEvent *e)
void Widget::mouseReleaseEvent(QMouseEvent *e)

c) Here we need to implement each event function in the widget.cpp file: (the specific implementation is shown below)

void Widget::mousePressEvent(QMouseEvent *e)
{
    qDebug() << " X , Y"; // output XY string
    qDebug() << tr("%1,%2").arg(e->x()).arg(e->y()); // e is the mouse object we got, we are in the object Get the relevant parameters of the mouse object, such as x-coordinate and y-coordinate
    ui->pushButton->setText(tr("(%1,%2)").arg(e->x()).arg(e->y())); // we will get the xy The coordinates are updated to the content of our Button
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{
    ui->pushButton_2->setText(tr("%1,%2").arg(e->x()).arg(e->y())); // same as above
} void Widget::mouseReleaseEvent(QMouseEvent *e) { ui->pushButton_3->setText(tr("%1,%2").arg(e->x()).arg(e->y())); // same as above }

d) We need to declare the related functions of the mouse we implemented in the header file widget.h file:

protected:
    void mousePressEvent(QMouseEvent *);

    void mouseMoveEvent(QMouseEvent *);

    void mouseReleaseEvent(QMouseEvent *);

 The above basically completes all the functions of the entire program. For the detailed engineering code, please refer to the content in my blog garden file ( file name MouseMonitor.tar.gz ): https://i.cnblogs.com/Files.aspx

Guess you like

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