JPFrancoia
JPFrancoia

Reputation: 5629

Menu entry not shown on Mac OS

I'm trying to port my PyQt program to Mac OS. I developed it on Linux.

PyQt (4) is installed on the 2 computers, with python 3.4. Everything seems to work. The program works perfectly on the Linux computer. I simply transfered it to the mac computer, and tried to run it. And it runs, except for only ONE thing:

I have a menu entry (in the menu bar), for the settings of the program:

# Action to show a settings window
self.settingsAction = QtGui.QAction('Settings', self)
self.settingsAction.triggered.connect(lambda: Settings(self))

...Some code...

# Menu entry for the settings
self.menubar.addAction(self.settingsAction)

On Linux, the 'Settings' entry is perfectly displayed in the menu bar, and opens the settings window. On Mac OS however, the Settings entry is not displayed. Simply not displayed. All the other entries (Files, Edit, View, etc) are displayed correctly, but not Settings. And there is no exception raised.

I'm stuck here, I never used Mac OS, so I don't even know where to start for debugging.

Do you have any suggestion ?

EDIT:

I also tried

# Action to show a settings window self.settingsAction = QtGui.QAction('Preferences', self) self.settingsAction.triggered.connect(lambda: Settings(self))

...Some code...

# Menu entry for the settings
self.menubar.addAction(self.settingsAction)

Upvotes: 5

Views: 3157

Answers (2)

yanzi1225627
yanzi1225627

Reputation: 963

Thanks @Greg. I want to add that on Mac, a menu must have at least one action, otherwise it will not show. Below is effects on Mac

Upvotes: 1

Greg
Greg

Reputation: 1629

Macs have strange behavior regarding menu bars. Remember that mac menubars are displayed in the system menubar at the very top, not in the window like on a Windows or Linux machine. One solution is to use a non-native menubar so it appears in the window similar to a Windows or Linux machine.

menubar = self.menuBar()
menubar.setNativeMenuBar(False)

Also, when using PyQt on a mac, the system will intercept certain commands contain the word 'Quit', 'Exit', 'Setting', 'Settings', 'preferences' and probably a slew of others, and remove them from your menubar because they are reserved labels. if a menubar header has no items, it will not display, making it appear as if you haven't modified the menubar.

#exit = QtGui.QAction( 'Exit', self ) #this fails on my system
exit = QtGui.QAction( 'SomethingElse', self ) #this displays on my system
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exit)

I found the 'Exit' information here: http://python.6.x6.nabble.com/addAction-to-menubar-td1916296.html

Also, calling raise_() doesn't change the menubar on my mac (Mavericks). I have to manually select the window (by clicking elsewhere then reclicking the window) to get the correct menubar to show for my pyqt app.

I hope this helps anyone else with these problems.

Upvotes: 13

Related Questions