PySide6 setStatusTip() 显示 QAction 名称

PySide6 setStatusTip() shows QAction name

我正在学习 PySide6,我偶然发现了一件奇怪的事情。 创建QAction并设置状态提示时,QAction的名称显示为状态提示,而不是实际的状态提示。

我在这里错过了什么?

这是我的简短示例代码:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Test App")
        label = QLabel("Hello!")
        label.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(label)

        toolbar = QToolBar("My main toolbar")
        self.addToolBar(toolbar)

        button_action = QAction("Test", self)
        button_action.setShortcut('Ctrl+T')
        button_action.setStatusTip('Test application')
        button_action.triggered.connect(self.onMyToolBarButtonClick)
        toolbar.addAction(button_action)

    def onMyToolBarButtonClick(self, s):
        print("click", s)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec()

这是奇怪的结果:

谢谢!

图中显示的是工具提示,默认情况下工具栏显示操作的text() as a tooltip, unless a tool tip is explicitly set using setToolTip()

状态提示显示在状态栏QStatusBar)。

在 QMainWindow 上,可以使用 statusBar() 访问状态栏(如果 none 存在,这是新的空 main windows 的默认设置,则创建并返回一个新的).
只需在 __init__() 中的任意位置添加以下内容,当悬停操作时,您会看到 "Test application" 字符串实际显示在那里:

        self.statusBar()

状态栏也可以安装在任何 QWidget 或继承的子类上,并可用于捕获从其自身或其子类收到的任何状态事件:

class MainWindow(QWidget): # note: a basic QWidget
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        button = QPushButton('Test')
        layout.addWidget(button)
        button.setStatusTip('Test application')
        self.statusBar = QStatusBar()
        layout.addWidget(self.statusBar)

    def event(self, event):
        if event.type() == event.Type.StatusTip:
            self.statusBar.showMessage(event.tip())
            # returning True means that the event has been 
            # successfully handled and will not be propagated to 
            # the possible parent(s)
            return True
        return super().event(event)

以上是 QMainWindow 在幕后所做的实际工作。