Mouse events (various processing events of the mouse)

The QMouseEvent class is used to represent a mouse event, which is generated when the mouse is pressed or the mouse pointer is moved in the widget. Using the QMouseEvent class, you can know which button the mouse is pressed, the current position of the mouse pointer, and other information. Usually redefine the mouse event handler of the widget to perform some custom operations.
The QWheelEvent class is used to represent the wheel event, mainly used to obtain the direction and distance of the wheel movement.
Sample code:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    Ui :: Widget * ui;
    QPoint offset; // used to store the difference between the mouse pointer position and the window position

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);

};

#endif // WIDGET_H

 
 
.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui (new Ui :: Widget)
{
    ui-> setupUi (this);
    QCursor cursor; // create cursor object
    cursor.setShape(Qt::OpenHandCursor); // set cursor shape
    setCursor(cursor); // use the cursor
}

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

void Widget::mousePressEvent(QMouseEvent *event) // mouse press event
{
    if(event->button() == Qt::LeftButton){ // If the left mouse button is pressed
        QCursor cursor;
        cursor.setShape(Qt::ClosedHandCursor);
        QApplication::setOverrideCursor(cursor); // temporarily change the shape of the mouse pointer
        offset = event->globalPos() - pos(); // Get the difference between the pointer position and the window position
    }
    else if(event->button() == Qt::RightButton){ // If the right mouse button is pressed
        QCursor cursor(QPixmap("../mymouseevent/logo.png"));
        QApplication::setOverrideCursor(cursor);// Use a custom image as the mouse pointer
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event) // mouse move event
{
    if(event->buttons() & Qt::LeftButton){ // buttons() must be used here
        QPoint temp;
        temp = event->globalPos() - offset;
        move(temp);// Subtract the difference from the current position of the mouse pointer to get the position where the window should move
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *event) // mouse release event
{
    Q_UNUSED(event);
    QApplication::restoreOverrideCursor(); // restore the mouse pointer shape
}

void Widget::mouseDoubleClickEvent(QMouseEvent *event) // mouse double click event
{
    if(event->button() == Qt::LeftButton){ // If the left mouse button is pressed
        if(windowState() != Qt::WindowFullScreen) // if not full screen now
            setWindowState(Qt::WindowFullScreen); // set the window to full screen
        else setWindowState(Qt::WindowNoState); // else restore the previous size
    }
}

void Widget::wheelEvent(QWheelEvent *event) // wheel event
{
    if(event->delta() > 0){ // when the scroll wheel moves away from the user
        ui->textEdit->zoomIn(); // zoom in
    }else{ // When the wheel rotates towards the user
        ui->textEdit->zoomOut(); // zoom out
    }
}



Guess you like

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