2

I'm using PyQt5. My problem is that the menubar cannot be clicked after the centralwidget has been added to the QMainWindow.

I'm using python 3.7 on Linux server.

Here's a MWE for the problem I'm having:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp, QWidget, QVBoxLayout


class Ui_MainWindow(object):

    def initUI(self,MyMainWindow):

        exitAction = QAction('Exit', MyMainWindow)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        menubar = MyMainWindow.menuBar()
        menubar.setNativeMenuBar(False)
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

        MyMainWindow.setWindowTitle('Menubar')
        MyMainWindow.setGeometry(300, 300, 300, 200)

        self.vbox = QVBoxLayout()
        self.centralwidget = QWidget(MyMainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.centralwidget.setLayout(self.vbox)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    MyMainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.initUI(MyMainWindow)
    MyMainWindow.show()
    sys.exit(app.exec_())

I've made the example so that its format is similar to the original code I'm working on. In its current form when I run the code, a menubar is shown but cannot be clicked on. However when the 4 lines for generating the layout and the centralwidget are disabled, the menubar works fine.

In my original code (which isn't shown here) the menubar isn't even showing up at all, and I'm not using Mac OS X, as I've seen in other peoples' questions. To me the only difference seems to be the existence of the centralwidget. Am I supposed to assign separate spaces for the menubar and the centralwidget, or some other configuration settings to make this work?

There is no error message, just that the menubar doesn't work/show on the MainWindow the code produces.

Any help will be greatly appreciated.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sam Kim
  • 23
  • 3

1 Answers1

0

To better understand the problem let's add some color:

# ...
self.centralwidget.setLayout(self.vbox)
self.centralwidget.setStyleSheet("background-color: yellow")
# ...

Before:

enter image description here

After:

enter image description here

As you can see the self.centralwidget widget is on the QMenuBar preventing the mouse events from reaching the QMenuBar.

And why does that happen? Is that QMainWindow already has a predefined layout:

enter image description here

So in this case you must place your self.centralwidget in the centralwidget of QMainWindow using the setCentralWidget() method:

# ...
self.vbox = QVBoxLayout()
self.centralwidget = QWidget(MyMainWindow)
self.centralwidget.setObjectName("centralwidget")
self.centralwidget.setLayout(self.vbox)
MyMainWindow.setCentralWidget(self.centralwidget) # <---
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you so much, that was the problem. I learned a debugging skill as well for problems in layouts. – Sam Kim Aug 07 '19 at 06:28