QToolTip 中包含的变量不会自动更新
Variables contained in a QToolTip not updating automatically
我在 QLineEdit 上有一个 QToolTip,工具提示在文本中包含变量。工具提示代码包含在 init.问题是工具提示中的变量值在程序运行中发生变化时不会自动更新。例如,我将鼠标悬停在行编辑上,值出现在工具提示中。我更改程序,返回行编辑,工具提示中的变量没有改变。
我可以通过将 .setToolTip 移动到一个函数并在每次程序中发生任何更改时调用该函数来解决这个问题,但这似乎有点矫枉过正,尤其是当 99% 的程序更改与这个特定的工具提示)。
变量应该自动更新吗?这是 init.
中包含的工具提示设置代码
self.ui.YourSSAmount.setToolTip(
'<span>Click Reports/Social Security to see your<br>SS income at each start age'
'<br><br>Your inf adj FRA amt at age {}: ${:,.0f}'
'<br>Age adjustment: {:.0f}%'
'<br>SS Income at age {}: ${:,.0f}</span>'.format(
self.generator.YouSSStartAge, self.generator.your_new_FRA_amt,
self.generator.SS66.get(self.generator.YouSSStartAge, 1.32) * 100, self.generator.YouSSStartAge,
self.generator.YourSSAmount))
setToolTip 方法获取文本并将其存储,如果用于构成文本的任何变量发生更改,则不会收到通知。
鉴于此,有 2 种可能的解决方案:
每次变量更改时更新工具提示:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.le = QtWidgets.QLineEdit()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.le)
self.foo = QtCore.QDateTime.currentDateTime().toString()
self.update_tooltip()
timer = QtCore.QTimer(self, timeout=self.on_timeout)
timer.start()
def on_timeout(self):
self.foo = QtCore.QDateTime.currentDateTime().toString()
# every time any variable used to build the tooltip changes
# then the text of the tooltip must be updated
self.update_tooltip()
def update_tooltip(self):
# update tooltip text
self.setToolTip("dt: {}".format(self.foo))
if __name__ == "__main__":
app = QtWidgets.QApplication([])
w = Widget()
w.show()
app.exec_()
覆盖工具提示以使用变量获取文本:
from PyQt5 import QtCore, QtWidgets
class LineEdit(QtWidgets.QLineEdit):
def __init__(self, parent=None):
super().__init__(parent)
self._foo = ""
@property
def foo(self):
return self._foo
@foo.setter
def foo(self, foo):
self._foo = foo
def event(self, e):
if e.type() == QtCore.QEvent.ToolTip:
text = "dt:{}".format(self.foo)
QtWidgets.QToolTip.showText(e.globalPos(), text, self, QtCore.QRect(), -1)
e.accept()
return True
return super().event(e)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.le = LineEdit()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.le)
self.le.foo = QtCore.QDateTime.currentDateTime().toString()
timer = QtCore.QTimer(self, timeout=self.on_timeout)
timer.start()
def on_timeout(self):
self.le.foo = QtCore.QDateTime.currentDateTime().toString()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
w = Widget()
w.show()
app.exec_()
我在 QLineEdit 上有一个 QToolTip,工具提示在文本中包含变量。工具提示代码包含在 init.问题是工具提示中的变量值在程序运行中发生变化时不会自动更新。例如,我将鼠标悬停在行编辑上,值出现在工具提示中。我更改程序,返回行编辑,工具提示中的变量没有改变。
我可以通过将 .setToolTip 移动到一个函数并在每次程序中发生任何更改时调用该函数来解决这个问题,但这似乎有点矫枉过正,尤其是当 99% 的程序更改与这个特定的工具提示)。
变量应该自动更新吗?这是 init.
中包含的工具提示设置代码self.ui.YourSSAmount.setToolTip(
'<span>Click Reports/Social Security to see your<br>SS income at each start age'
'<br><br>Your inf adj FRA amt at age {}: ${:,.0f}'
'<br>Age adjustment: {:.0f}%'
'<br>SS Income at age {}: ${:,.0f}</span>'.format(
self.generator.YouSSStartAge, self.generator.your_new_FRA_amt,
self.generator.SS66.get(self.generator.YouSSStartAge, 1.32) * 100, self.generator.YouSSStartAge,
self.generator.YourSSAmount))
setToolTip 方法获取文本并将其存储,如果用于构成文本的任何变量发生更改,则不会收到通知。
鉴于此,有 2 种可能的解决方案:
每次变量更改时更新工具提示:
from PyQt5 import QtCore, QtWidgets class Widget(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.le = QtWidgets.QLineEdit() lay = QtWidgets.QVBoxLayout(self) lay.addWidget(self.le) self.foo = QtCore.QDateTime.currentDateTime().toString() self.update_tooltip() timer = QtCore.QTimer(self, timeout=self.on_timeout) timer.start() def on_timeout(self): self.foo = QtCore.QDateTime.currentDateTime().toString() # every time any variable used to build the tooltip changes # then the text of the tooltip must be updated self.update_tooltip() def update_tooltip(self): # update tooltip text self.setToolTip("dt: {}".format(self.foo)) if __name__ == "__main__": app = QtWidgets.QApplication([]) w = Widget() w.show() app.exec_()
覆盖工具提示以使用变量获取文本:
from PyQt5 import QtCore, QtWidgets class LineEdit(QtWidgets.QLineEdit): def __init__(self, parent=None): super().__init__(parent) self._foo = "" @property def foo(self): return self._foo @foo.setter def foo(self, foo): self._foo = foo def event(self, e): if e.type() == QtCore.QEvent.ToolTip: text = "dt:{}".format(self.foo) QtWidgets.QToolTip.showText(e.globalPos(), text, self, QtCore.QRect(), -1) e.accept() return True return super().event(e) class Widget(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.le = LineEdit() lay = QtWidgets.QVBoxLayout(self) lay.addWidget(self.le) self.le.foo = QtCore.QDateTime.currentDateTime().toString() timer = QtCore.QTimer(self, timeout=self.on_timeout) timer.start() def on_timeout(self): self.le.foo = QtCore.QDateTime.currentDateTime().toString() if __name__ == "__main__": app = QtWidgets.QApplication([]) w = Widget() w.show() app.exec_()