Search code examples
pysidepyside6

String containing "about" cannot be added to the menu in PySide6


I am attempting to add some actions to menu of menuBar in PySide6, but string containing "about" is not added. Here is my sample code:

from PySide6 import QtWidgets


class MyGUI(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()
        #
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        fileMenu.addAction("test", self.test)  # OK
        fileMenu.addAction("A-bout", self.test)  # OK
        # string containing "about" cannot be added to menu
        fileMenu.addAction("About", self.test)  # cannot be added
        fileMenu.addAction("ABOUT", self.test)  # cannot be added
        fileMenu.addAction("about", self.test)  # cannot be added
        fileMenu.addAction("_About", self.test)  # cannot be added
        #
        self.show()

    def test(self):
        print("test")


if __name__ == '__main__':
    app = QtWidgets.QApplication()
    ex = MyGUI()
    app.exec()

Result image: Mac

Did I do something wrong. Thanks very much.


Solution

  • As explained in the documentation:

    On macOS [...], QMenuBar is a wrapper for using the system-wide menu bar.
    [...]
    Qt for macOS also provides a menu bar merging feature to make QMenuBar conform more closely to accepted macOS menu bar layout.

    The documentation also shows a list of string matches and placements. The about actions you're not seeing are actually in the application menu (the one named "Python" in your case).

    If you want to override that behavior, you have to explicitly set the menu role for that action:

        aboutAction = fileMenu.addAction("About", self.test)
        aboutAction.setMenuRole(QAction.MenuRole.NoRole)
    

    Note, as the docs also point out:

    The menu role can only be changed before the actions are put into the menu bar in macOS (usually just before the first application window is shown).

    Meaning that this cannot be changed at runtime after the window is shown.