[PyQt] Show tooltip onto QLabel

Maurizio Berti maurizio.berti at gmail.com
Mon Jan 21 23:40:25 GMT 2019

Using standard tooltips might be an issue in some (not so special) cases,
as QToolTip events are strictly related to the widget events, so you'll
need to install an eventFilter on every single widget you'll want the
behavior you ask about.
A simple implementation would be something like this:

    def __init__(self, *args, **kwargs):
        self.toolTipWidget = QtWidgets.QLabel()
        self.someWidget = SomeWidget()
        self.someWidget.setToolTip('I am a tooltip!')

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.ToolTip:
        return QtWidgets.QWidget.eventFilter(self, source, event)

On the other hand, I'd suggest to use the StatusTip instead of the ToolTip:
it's something I've successfully used for something similar to your needs:
in this way you can keep the StatusTip for "simple" tooltip text and the
ToolTips for further customized messages, as they also allow rich text
Here's a small example:

    def __init__(self, *args, **kwargs):
        self.toolTipWidget = QtWidgets.QLabel()
        self.someWidget = SomeWidget()
        self.someWidget.setStatusTip('I am a statustip!')

    def event(self, event):
        if event.type() == QtCore.QEvent.StatusTip and event.tip():
        return QtWidgets.QWidget.event(self, event)

Note that here I used the setStatusTip() method instead of the setToolTip()
Obviously, if you're using Designer you'll set the StatusTip property from
there, instead of the ToolTip property.

How does it work?
Usually the StatusTip is only used on a QMainWindow with a QStatusBar
installed, but, interestingly enough, the event() method of *any* widget
can be used to catch *any* StatusTip event called from both that widget
*and* its children.

In both cases you'd better think about a way to "clear" the tool/status
tip, as leaving it there might be confusing.
If you don't have too many widgets, the eventFilter way might be a good
solution, as you can also catch the QEvent.Leave event type to hide the
"tooltip" by clearing the label text, otherwise it's probably better using
a QTimer on the parent widget or the label, and set it as singleShot (don't
use the static method, as it could hide a new statustip activated in the
meantime), then connect it to something like lambda:
self.toolTipWidget.setText('') whenever you catch the StatusTip event.

If for some reason you'll need to stick with ToolTips, there's a solution
If all widgets already have their tooltips, and the layout is static and
permanent once the main parent widget is being instantiated, just use the
children() iterator and check for both isWidgetType() and toolTip()
contents: if those condition match, install the eventFilter; if the layout
is dynamic instead, use the childEvent method on the parent and check for
QEvent.childAdded and QEvent.childRemoved events, then use
installEventFilter or removeEventFilter respectively.


Il giorno lun 21 gen 2019 alle ore 23:22 Tong Zhang <warriorlance at gmail.com>
ha scritto:

> Hello,
> Can I show the tooltip of some widget onto a QLabel? e.g. Tooltip will
> show when I move the mouse onto the pushbutton, how about show the tooltip
> on another widget, say label?
> Thanks,
> Tong
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190122/2046865f/attachment.html>

More information about the PyQt mailing list