QGraphicsProxyWidg中小部件的工具提示

2024-09-19 23:31:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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")

提前谢谢!在


Tags: 工具代码selfviewinit标志buttonscene
2条回答

必须将工具提示设置为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_())

enter image description here

不幸的是,在代理上设置工具提示也不起作用,但是一位同事在我的代码中进一步研究,发现了为什么proxy和button的工具提示都不起作用:我在QGraphicsView mouseMoveEvent上设置了一个覆盖,这可能会破坏悬停事件。正在添加。。。在

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

…在mouseMoveEvent重写的末尾解决了这个问题。很抱歉弄错了,谢谢你的帮助!在

相关问题 更多 >