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.