激活标签时的鼠标指针位置

时间:2021-02-16 20:47:03

标签: c++ qt events mouse

QLabel 子类的对象被激活时,如何判断鼠标指针是否在标签上,如果是,如何获取它的位置?

QWidegt::event() 可以检查 QEvent::WindowActivate 的事件类型,但不提供鼠标指针位置信息。

更新

根据@Mathias Schmid 的评论,我创建了以下代码。它自行验证 focusInEvent 和 focusOutEvent 都可能发生。但是,我仍然无法获得鼠标指针位置。也许我错过了“绑定启用/禁用鼠标跟踪以聚焦进出事件”或其他内容。

#include "mainwindow.h"

#include <QApplication>
#include <QtWidgets>
#include <QtCore>

class MyLabel : public QLabel
{
public:
    MyLabel(QWidget*parent = nullptr) : QLabel(parent)
    {
        setMouseTracking(true);
        setFocusPolicy(Qt::FocusPolicy::StrongFocus);
    }

protected:
    virtual void focusInEvent(QFocusEvent *ev) override
    {
        (void)ev;
        this->setText(__PRETTY_FUNCTION__);
    }
    virtual void focusOutEvent(QFocusEvent *ev) override
    {
        (void)ev;
        this->setText(__PRETTY_FUNCTION__);
    }

    virtual void mouseMoveEvent(QMouseEvent *ev) override
    {
        this->setText(QString::number(ev->pos().x()) +", " +QString::number(ev->pos().y()));

        QLabel::mouseMoveEvent(ev);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyLabel w;
    w.setFixedSize(400, 300);
    w.show();
    return a.exec();
}

更新 2:解决方案

感谢@Mathias Schmid,最终的解决方案是在 focusInEvent() 中使用静态函数 QCursor::pos()。

#include "mainwindow.h"

#include <QApplication>
#include <QtWidgets>
#include <QtCore>
#include <QCursor>

class MyLabel : public QLabel
{
public:
    MyLabel(QWidget*parent = nullptr) : QLabel(parent)
    {
        setMouseTracking(true);
        setFocusPolicy(Qt::FocusPolicy::StrongFocus);
    }

protected:
    virtual void focusInEvent(QFocusEvent *ev) override
    {
        (void)ev;
        QPoint pos = QCursor::pos();
        QString msg = QString(__PRETTY_FUNCTION__) + ": \n" +  QString::number(pos.x()) + ", " + QString::number(pos.y());
        QPoint posLocal = this->mapFromGlobal(pos);
        if(this->rect().contains(posLocal))
            msg += "\nLocal pos: " + QString::number(posLocal.x()) + ", " + QString::number(posLocal.y());

        this->setText(msg);

        QLabel::focusInEvent(ev);
    }
    virtual void focusOutEvent(QFocusEvent *ev) override
    {
        (void)ev;
        this->setText(QString(__PRETTY_FUNCTION__));

        QLabel::focusOutEvent(ev);
    }

    virtual void mouseMoveEvent(QMouseEvent *ev) override
    {
        this->setText(QString::number(ev->pos().x()) +", " +QString::number(ev->pos().y()));

        QLabel::mouseMoveEvent(ev);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyLabel w;
    w.setFixedSize(450, 300);
    w.show();
    return a.exec();
}

1 个答案:

答案 0 :(得分:1)

要在获得焦点时检查鼠标光标是否在派生自 QLabel 的自定义小部件上,只需覆盖 focusInEvent() 并在那里处理鼠标光标位置检查。

MyLabel.h

class MyLabel: public QLabel
{
    Q_OBJECT

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

protected:
    virtual void focusInEvent(QFocusEvent *event) override;
};

MyLabel.cpp

MyLabel::MyLabel(QWidget *parent)
    : QLabel(parent)
{
    setFocusPolicy(Qt::StrongFocus);
}

MyLabel::~MyLabel()
{
}

void MyLabel::focusInEvent(QFocusEvent *event)
{
    if (event) {
        const QPoint cursorPos = QCursor::pos();
        if (rect().contains(mapFromGlobal(cursorPos))) {
            // TODO: Add desired action
        }
        QLabel::focusInEvent(event);
    }
}
相关问题