持续更新提示 [英] continuously updating tooltip
本文介绍了持续更新提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法持续更新 QLabel(或其他)的工具提示?
is there a way to update the tooltip of a QLabel (or whatever) continuously?
例如下面的代码使用一个持续更新标签及其工具提示的计时器.虽然我可以看到标签的变化,但如果我将鼠标悬停在 QLabel 上,我会得到一个带有最后一个当前值的工具提示.工具提示保持固定",直到我移动鼠标,将工具提示更新为其新值.
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?
推荐答案
嗯,这并不容易,但这里的代码应该可以满足您的需求:
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_())
这篇关于持续更新提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文