Reputation: 2520
I have a QGraphicsScene
and inside I have a few QGraphicsItem
s. 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
Upvotes: 1
Views: 3579
Reputation: 71
Just in case anyone stumbles upon this question, the solution is to call the tooltip via QToolTip.showText() and use the following constructor:
static PySide2.QtWidgets.QToolTip.showText(pos, text, w, rect, msecShowTime)
where msecShowTime can be specified to be very long, e.g. 999999, to avoid the tooltip from disappearing.
Upvotes: 0
Reputation: 29621
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
Upvotes: 1