QToolTip中包含的变量不会自动更新

2024-09-27 20:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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 {}:&nbsp;&nbsp;${:,.0f}'
        '<br>Age adjustment:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{:.0f}%'
        '<br>SS Income at age {}:&nbsp;&nbsp;&nbsp;&nbsp;${:,.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))

Tags: 工具函数代码brself程序编辑age
1条回答
网友
1楼 · 发布于 2024-09-27 20:18:47

setToolTip方法获取文本并存储它,如果用于形成文本的任何变量发生更改,则不会收到通知

鉴于此,有两种可能的解决方案:

  • 每次变量更改时更新工具提示:

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

相关问题 更多 >

    热门问题