Qt鼠标事件mouseEvent

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QWidget>
#include <QDebug>
#include <QMouseEvent>
#include <QLabel>

namespace Ui {
class MyLabel;
}

class MyLabel : public QLabel
{
    Q_OBJECT

public:
    explicit MyLabel(QWidget *parent = nullptr);
    ~MyLabel();

    void mousePressEvent(QMouseEvent *event) override
    {
        qDebug() << "mousePressEvent";
    }

    void mouseReleaseEvent(QMouseEvent *event) override
    {
        qDebug() << "mouseReleaseEvent";
    }

    void mouseMoveEvent(QMouseEvent *event) override
    {
        qDebug() << "x" << event->x();
        qDebug() << "y" << event->y();
        qDebug() << "globalX" << event->globalX();
        qDebug() << "globalY" << event->globalY();
    }

private:
    Ui::MyLabel *ui;
};

#endif // MYLABEL_H

/* 鼠标按下、移动、释放 */
21:58:16: Starting ...
mousePressEvent
x 18
y 29
globalX 928
globalY 549
x 20
y 29
globalX 930
globalY 549
x 22
y 29
globalX 932
globalY 549
x 23
y 29
globalX 933
globalY 549
x 25
y 30
globalX 935
globalY 550
mouseReleaseEvent
posted @ 2022-08-03 22:03  thomas_blog  阅读(133)  评论(0编辑  收藏  举报