PyQt MenuBar Mac OSX Snow Leopard

JeremyFromEarth picture JeremyFromEarth · Feb 14, 2010 · Viewed 7k times · Source

I am attempting to add an item to the application menu-bar of a simple PyQt example. However, the following code does not seem to alter the menu-bar at all. The only item in the menu is "Python". Below is the bulk of the code, minus imports and instantiation.

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

        self.resize(250, 150)
        self.setWindowTitle('menubar')
        self.modal = False

        exit = QtGui.QAction( QtGui.QIcon('images/app_icon.png'), 'Exit', self )
        exit.setShortcut('Ctrl+Q')
        exit.setStatusTip('Exit application')
        self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

        menubar = self.menuBar()
        file = menubar.addMenu('File')
        file.addAction(exit)

I've also tried creating a new QMenuBar and using the setMenuBar() method to manually swap out the menu bar.

Any glaring mistakes in the above snippet?

Answer

tftdias picture tftdias · Feb 21, 2014

I know this question is old but, since I was stuck with the same problem, I found that because I was creating an action to quit the application and this action is reserved on OSX to the Application Menu, the File menu did not appear. As I created a new action on the same menu, it became available. This worked by using the same approach for other OS's:

self.menubar = self.menuBar()

This was created inside a QMainWindow object.

Hope this helps anyone!