Search code examples
pythonqtpyqt4windows-8.1qmenubar

QMenuBar missing from QMainWindow on Windows 8.1


I am creating a QMainWindow with PyQt4 on Windows 8.1. However, the default QMenuBar fails to show after adding menus to it. Here is my SSCCE:

import sys
from PyQt4.Qt import *    

class NoMenuBar(QMainWindow):

    def __init__(self):
        super(NoMenuBar, self).__init__()

        self.__initUI()

    def __initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('No Menu Bar')
        self.__configureMenuBar()

    def __configureMenuBar(self):
        self.menuBar = self.menuBar()
        self.menuBar.addMenu(QMenu('File'))
        self.menuBar.addMenu(QMenu("Settings"))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = NoMenuBar()
    window.show()
    sys.exit(app.exec_()) 

Solution

  • The QMenu needs to be passed the parent window as an argument (which is self in this case).

    def __configureMenuBar(self):
        self.menuBar = self.menuBar()
        self.menuBar.setLayoutDirection(Qt.LeftToRight)
        filemenu = QMenu('File', self)
        self.menuBar.addMenu(filemenu)
        filemenu.addAction('Open')
        filemenu.addAction('Quit', self.close)