QGraphicsProxyWidget 内小部件的工具提示

Tooltip for widget inside a QGraphicsProxyWidget

我有一个 QGraphicsScene,其中我在 QGraphicsProxyWidgets 中添加了 QPushButtons。有没有办法显示这些按钮的工具提示? setToolTip 有效,但当我将鼠标悬停在按钮上时什么也没有出现。我需要在 QGraphicsScene/View 上指定一些标志吗?

按钮创建代码的简化版本:

class Button(QPushButton):

def __init__(self, scene):

    super(Button, self).__init__()

    self.proxy = QGraphicsProxyWidget()
    self.proxy.setWidget(self)
    scene.addItem(self.proxy)

    self.setToolTip("tooltip")

提前致谢!

您必须将工具提示设置为 QGraphicsProxyWidget

示例:

from PyQt5 import QtCore, QtWidgets

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    scene = QtWidgets.QGraphicsScene()
    view = QtWidgets.QGraphicsView(scene)
    proxy = QtWidgets.QGraphicsProxyWidget()
    button = QtWidgets.QPushButton("Press me :)")
    proxy.setWidget(button)
    proxy.setToolTip("Proxy toolTip")
    scene.addItem(proxy)
    view.show()
    sys.exit(app.exec_())

遗憾的是,在代理上设置工具提示也不起作用,但一位同事进一步查看了我的代码,发现了为什么代理和按钮的工具提示都不起作用:我在 QGraphicsView mouseMoveEvent,这可能会破坏悬停事件。添加...

else :
    super(CustomGraphicsView, self).mouseMoveEvent(event)

...在 mouseMoveEvent 覆盖的末尾解决了问题。抱歉弄错了,感谢您的帮助!