[PyQt] Working example of QMenuBar on OSX? Examples don't work

David Cortesi davecortesi at gmail.com
Sun Jun 5 21:01:16 BST 2016

Ah. How are you running this program? e.g. from a command line, python
myscript.py , or how?

What you describe sounds  like a problem with an app after it was bundled
by PyInstaller. Owing to the lack of some magical incantation in the app's
info.plist, the app's menus would not appear when it was launched, but if
it was hidden (or if another app was made active), when it came again to
the front, its menus did appear.

That has been fixed for PyInstaller with the latest (3.2) version. Anyway,
the code I showed you should work.

On Sun, Jun 5, 2016 at 11:57 AM, Marcel Maré <list at webtothemax.com> wrote:

> Hi
> Think I found out some more.
> To rule out a programming error on my part (bering new to Qt) I only
> looked at the menus.py example (Qt/examples/mainwindows/menus.py).
> The relevant menu definition sections is like this:
> def createMenus(self):
>     self.fileMenu = self.menuBar().addMenu("&File")
>     self.fileMenu.addAction(self.newAct)
>     self.fileMenu.addAction(self.openAct)
>     self.fileMenu.addAction(self.saveAct)
>     self.fileMenu.addAction(self.printAct)
>     self.fileMenu.addSeparator()
>     self.fileMenu.addAction(self.exitAct)
>     self.editMenu = self.menuBar().addMenu("&Edit")
>     self.editMenu.addAction(self.undoAct)
>     self.editMenu.addAction(self.redoAct)
>     self.editMenu.addSeparator()
>     self.editMenu.addAction(self.cutAct)
>     self.editMenu.addAction(self.copyAct)
>     self.editMenu.addAction(self.pasteAct)
>     self.editMenu.addSeparator()
> So, since it uses self.menuBar() it should not be compatible with the
> system menu on OSX, since it does not use QMenuBar().
> Wat I found out that is that when I run the program the menu is visible
> but unclickable (as reported earlier). However, when I switch to some other
> program and then back to the menus.py program the menu works!
> Looks like a bug to me.
> Marcel
> On 05 Jun 2016, at 18:42, David Cortesi <davecortesi at gmail.com> wrote:
> I have a PyQt5 app that works well in Mac OS, Linux and Windows. The only
> difference I see in the setup of my QMainWindow is this:
>         import platform
>         if platform.uname().system.startswith('Darw') :
>             self._menu_bar = QMenuBar() # parentless menu bar for Mac OS
>         else :
>             self._menu_bar = self.menuBar() # refer to the default one
> and then it proceeds to initialize self._menu_bar with menus, e.g.
>         self.file_menu = self._menu_bar.addMenu(_TR('Menu name', '&File'))
>         work = self.file_menu.addAction( _TR('File menu command','&New') )
>         work.setShortcut(QKeySequence.New)
>         work.setToolTip( _TR('File:New tooltip','Create a new, empty
> document') )
>         work.triggered.connect(self._new)
> and so forth. The above test of platform.uname() is about the only
> platform difference in the whole app...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160605/de997076/attachment.html>

More information about the PyQt mailing list