Qt Tooltip how to prevent the text from disappeari

2019-08-09 09:08发布

问题:

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