Search code examples
pythontooltippyside

continuously updating tooltip


is there a way to update the tooltip of a QLabel (or whatever) continuously?

e.g. the following code uses a timer that continuously updates a label and its tooltip. while i can see the label change, if i hover over the QLabel i will get a tooltip with the last current value. the tooltip stays "fixed", until i move the mouse, which updates the tooltip to it's new value.

!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.value=0
        self.initUI()
    def initUI(self):      
        hbox = QtGui.QHBoxLayout(self)
        self.lbl = QtGui.QLabel(self)
        self.lbl.setText("foo")
        self.lbl.setToolTip("bar")
        self.timer = QtCore.QBasicTimer()
        self.timer.start(100, self)
        hbox.addWidget(self.lbl)
        self.setLayout(hbox)
        self.show()
    def timerEvent(self, x):
        self.value=self.value+1
        self.lbl.setText("foo: %03d" % self.value)
        self.lbl.setToolTip("bar: %03d" % self.value)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

is there a way to update the tooltip without having to move the mouse?


Solution

  • Well it wasn't easy, but here is the code that should do what you want:

    !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import sys
    from PySide import QtGui, QtCore
    
    class Example(QtGui.QWidget):
        def __init__(self):
            super(Example, self).__init__()
            self.initUI()
        def initUI(self):      
            hbox = QtGui.QHBoxLayout(self)
            self.lbl = MyLabel(self)
            self.lbl.setText("foo")
            self.lbl.setToolTip("bar")
            hbox.addWidget(self.lbl)
            label2 = QtGui.QLabel('another label')
            hbox.addWidget(label2)
            label2.setToolTip('a normal tooltip')
            self.setLayout(hbox)
            self.show()
    
    
    class MyLabel(QtGui.QLabel):
        def __init__(self,*args,**kwargs):
            QtGui.QLabel.__init__(self,*args,**kwargs)
            self._timer = QtCore.QBasicTimer()
            self._timer.start(100, self)
            self._value = 0
            self._last_event_pos = None
    
        def event(self,event):
            if event.type() == QtCore.QEvent.ToolTip:
                self._last_event_pos = event.globalPos()
                return True
            elif event.type() == QtCore.QEvent.Leave:
                self._last_event_pos = None
                QtGui.QToolTip.hideText()
            return QtGui.QLabel.event(self,event)
    
        def timerEvent(self, x):
            self._value += 1
            if self._last_event_pos:
                QtGui.QToolTip.hideText()
                QtGui.QToolTip.showText(self._last_event_pos, "bar: %03d" % self._value)
            self.setText("foo: %03d" % self._value)
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())