将鼠标悬停在 QMenuBar 中的按钮上后,StatusBar 中写入的消息会更改,但当鼠标离开按钮时不会恢复

Message written in StatusBar is changed after hovering over a button in QMenuBar but is not reverted when the mouse leaves the button

我目前正在设计一个应用程序(作为一种更好地学习 PyQt5 的练习,因为我是 Qt 的新手),我在其中有一个显示消息的状态栏。最近,我决定在QMenuBar中使用QAction对象实现一个保存功能,并设置它的状态提示。

我的问题是当我将鼠标悬停在按钮上时它会显示提示,但是当鼠标离开按钮时,我最初为 statusBar 设置的消息消失了。但是,我希望能够保留我为 statusBar 设置的消息。

这是最小的可重现示例:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Example(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        saveAct = QtWidgets.QAction('&Save', self)
        saveAct.setShortcut('F5')
        saveAct.setStatusTip('Saves your file')
        self.statusBar().showMessage("Hello")

        self.menuBar().addAction(saveAct)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Simple menu')
        self.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

作为文档 explains:

Normal and Permanent messages are displayed by creating a small widget (QLabel, QProgressBar or even QToolButton) and then adding it to the status bar using the addWidget() or the addPermanentWidget() function.

解决方案是创建一个 QLabel 并将其添加到工具栏。

class Example(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        saveAct = QtWidgets.QAction('&Save', self)
        saveAct.setShortcut('F5')
        saveAct.setStatusTip('Saves your file')
        <b>self.normalMessage = QtWidgets.QLabel('Hello')
        self.statusBar().addWidget(self.normalMessage)</b>

        self.menuBar().addAction(saveAct)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Simple menu')
        self.show()