如何在 qt graphicsview 中定位工具提示

how to position a tooltip in qt graphicsview

我想在使用鼠标指针(无悬停)将线的端点拖动到 QGraphicsItem 时显示工具提示。

所以我有鼠标事件,我想调用 QToolTip::showText(QPoint pt, String str) 并且我想在相对于鼠标指针的正常位置显示工具提示。

但是无论我在 event.pos() 上尝试 (widget.mapToGlobal, item.mapToScene),工具提示在我的屏幕或图形视图的左上角显示为真实的,而不是鼠标。

也许我遗漏了一些简单的东西,但我真的很感激能得到一些帮助。

widget = item.scene().views()[0] #there is only one view
p = event.pos()
pt = widget.mapToGlobal(QtCore.QPoint(p.x(), p.y()))
QtGui.QToolTip.showText(pt, text) 

QGraphicsSceneMouseEvent 有另一个有用的方法 - screenPos() 其中 returns 鼠标光标在场景坐标中的位置。尝试使用它。例如:

QtGui.QToolTip.showText(event.screenPos(), text)