views:

326

answers:

2

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?

+2  A: 

I don't have PyQt installed on this machine to test this out, but I think on a Mac the QMainWindow.menuBar() function does not return the application wide menu bar. You might try creating menubar like.

menubar = QtGui.MenuBar()

I'm basing this on the docs for the QMainWindow.menuBar() function here:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmainwindow.html#menuBar

You might also check out the section labeled QMenuBar on Mac OS X on this page:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmenubar.html#details

Hope that helps!

ryan_s