我在QToolTip上有一个QLineEdit,工具提示中包含文本中的变量。工具提示代码包含在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))发布于 2020-07-23 17:26:36
setToolTip方法接受文本并存储它,如果用于形成文本更改的变量中有任何变量,则不会收到通知。
有鉴于此,有两种可能的解决办法:
每次变量更改时,
从PyQt5导入QtCore,QtWidgets类Widget(QtWidgets.QWidget):def __init__(self,parent=None):super().__init__(父级) 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() #每次用于构建工具提示的变量更改#,那么工具提示的文本必须更新self.update_tooltip() def update_tooltip(self):# update工具提示文本self.setToolTip("dt:{}“)“.format(self.foo)”如果__name__ == "__main__":app = QtWidgets.QApplication([]) w= Widget() w.show() app.exec_()
从PyQt5导入QtCore,QtWidgets class LineEdit(QtWidgets.QLineEdit):def __init__(self,parent=None):Super().__init__(父) self._foo = "“@property def foo(self):返回self._foo @foo.setter def foo(self,foo):self._foo = foo def事件(self,( e):如果e.type() == QtCore.QEvent.ToolTip: text = "dt:{}".format(self.foo) QtWidgets.QToolTip.showText(e.globalPos(),text,self,QtCore.QRect(),-1) e.accept()返回真返回超级().event(E)类Widget(QtWidgets.QWidget):def __init__(self,( parent=None):.__init__(父级) 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()如果__name__ == "__main__":app = QtWidgets.QApplication([]) w= Widget() w.show() app.exec_()
https://stackoverflow.com/questions/63053912
复制相似问题