continuously updating tooltip

偶尔善良 提交于 2019-12-11 13:11:59

问题


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?


回答1:


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_())



回答2:


so following @three_pineapples original proposal, this is what i came up with:

  • override the setToolTip() function to call hideText();showText(), but only
  • if the mouse-pointer is currently hovering over the label (detected in event())

here's the code:

class MyLabel(QtGui.QLabel):
    def __init__(self,*args,**kwargs):
        QtGui.QLabel.__init__(self,*args,**kwargs)
        self._setToolTip=QtGui.QLabel.setToolTip
        self._last_event_pos = None
        self._tooltip=QtGui.QLabel.toolTip(self)
    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 setToolTip(self, tt):
        self._setToolTip(self, tt)
        if self._last_event_pos:
            QtGui.QToolTip.hideText()
            QtGui.QToolTip.showText(self._last_event_pos,
                                    QtGui.QLabel.toolTip(self))


来源:https://stackoverflow.com/questions/19427625/continuously-updating-tooltip

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