激活标签时的鼠标指针位置
Mouse pointer position when a label is activated
当QLabel sub class的一个对象被激活时,如何找到鼠标指针是否在标签上,如果是,如何获取它的位置?
QWidegt::event() 可以检查 QEvent::WindowActivate 的事件类型,但它不提供有关鼠标指针位置的信息。
更新
根据@Mathias Schmid 的评论,我创建了以下代码。它会自行验证 focusInEvent 和 focusOutEvent 都可能发生。但是,我仍然无法获取鼠标指针位置。也许我错过了“绑定 enable/disable 鼠标跟踪以聚焦进出事件”或其他内容的部分。
#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();
}
要在获得焦点时检查鼠标光标是否位于从 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);
}
}
当QLabel sub class的一个对象被激活时,如何找到鼠标指针是否在标签上,如果是,如何获取它的位置?
QWidegt::event() 可以检查 QEvent::WindowActivate 的事件类型,但它不提供有关鼠标指针位置的信息。
更新
根据@Mathias Schmid 的评论,我创建了以下代码。它会自行验证 focusInEvent 和 focusOutEvent 都可能发生。但是,我仍然无法获取鼠标指针位置。也许我错过了“绑定 enable/disable 鼠标跟踪以聚焦进出事件”或其他内容的部分。
#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();
}
要在获得焦点时检查鼠标光标是否位于从 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);
}
}