qt:显示鼠标位置,如工具提示 [英] qt : show mouse position like tooltip
问题描述
在我写标题的时候,我希望像工具提示一样显示鼠标位置。
为此,我认为我必须覆盖QCursor,对吗?
但是我不知道QCursor的细节以及如何创建一个新的cursorShape。
有这样的例子吗?
As I write on title, I hope to show mouse position like tooltip.
To do that, I think that I have to override QCursor, right?
But I don't know the details of QCursor and how to make a new cursorShape.
Is there any example like this?
推荐答案
假设在移动光标时需要坐标读出(例如在许多图形或CAD应用程序中),您真的不要覆盖 QCursor
。
Assuming you want a coordinate readout as you move the cursor (like in many graphics or CAD applications), you really do not want to override QCursor
.
最有效的方法取决于提供坐标的小部件,但在大多数情况下最简单的方法是 setMouseTracking (true)
,并覆盖它 mouseMoveEvent(QMouseEvent * event)
以显示 QToolTip
像这样:
The most efficient approach depends on what widget will be providing the coordinates, but in most cases the simplest way will be to setMouseTracking(true)
for the widget, and override it's mouseMoveEvent(QMouseEvent* event)
to display a QToolTip
like so:
void MyWidget::mouseMoveEvent(QMouseEvent* event)
{
QToolTip::showText(event->globalPos(),
// In most scenarios you will have to change these for
// the coordinate system you are working in.
QString::number( event->pos().x() ) + ", " +
QString::number( event->pos().y() ),
this, rect() );
QWidget::mouseMoveEvent(event); // Or whatever the base class is.
}
通常你不会强制您可以使用 QWidget :: setToolTip(const QString&)
或在 QWidget :: event(QEvent *)中捕获工具提示事件
。但是正常 QToolTip
只会在短暂延迟后出现,但您希望它们不断更新。
Normally you would not 'force' a tooltip like this; you would use QWidget::setToolTip(const QString&)
or capture tooltip events in QWidget::event(QEvent*)
. But normal QToolTip
s only appear after short delay, but you want them updated continuously.
没有尝试过,但这是我会这样做的方式。希望有所帮助!
I should state that I haven't tried this, but this is the way I would do it. Hope that helps!
这篇关于qt:显示鼠标位置,如工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!