Qt Tooltip how to prevent the text from disappearing after the builtin delay?

我只是一个虾纸丫 提交于 2019-12-11 11:44:42

问题


I have a QGraphicsScene and inside I have a few QGraphicsItems. When I hover over a QGraphicsRectItem, I want to show an overlay text immediately. Only when the cursor leaves the item, then the text can disappear.

Below you can see I tried using whatsthis (which crashes python) and tooltip.

With tooltip, I am able to make the text appear immediately but the text disappears on its own after the built-in delay.

class Node(QGraphicsRectItem):
    def __init__(self, x, y, w, h, qpen, qbrush, text):
        QGraphicsRectItem.__init__(self)

        self.setRect(x, y, w, h)
        self.setBrush(qbrush)
        self.setPen(qpen)

        self.setAcceptHoverEvents(True)
        self.text = text

        #self.setFlag(QGraphicsItem.ItemIsMovable)
        #self.toolkit = QToolTip()
        #self.setToolTip(text)
        #self.setWhatsThis(self.text)

    def hoverEnterEvent(self, event):
        QToolTip.showText(event.screenPos(),self.text)

        #print("hoverEnterEvent : {}".format(event))
        #print(type(self.toolTip))
        #self.QToolTip.showText(event.pos(),text)
        #event.ToolTip.showText(text)

        #QWhatsThis.showText(event.screenPos(),self.text)
        #self.enterWhatsThisMode()

    def hoverLeaveEvent(self, event):
        QToolTip.hideText()

        #print("hoverLeaveEvent : {}".format(event))
        #self.QToolTip.hideText()
        #event.ToolTip.hideText()

        #QWhatsThis.hideText()
        #self.leaveWhatsThisMode()

I am using python 3.3 and pyside


回答1:


I thought this SO post and this page might be your answer. But as you point out, a comment at bottom of page indicates this only works for controlling how soon tooltip appears, not how long it remains visible. Unfortunately, that link to bug item no longer exists (see also Keep Qt tooltip open). The closest I found is https://bugreports.qt-project.org/browse/QTBUG-31707 which hasn't been assigned to anyone, which suggests that you either have to find a library that provides what you want or, short of that, you have to implement your own. For the latter, you might want to look at QxtToolTip or example



来源:https://stackoverflow.com/questions/21356035/qt-tooltip-how-to-prevent-the-text-from-disappearing-after-the-builtin-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!