pythonpyqtpyqt5qmainwindowqmenubar

MenuBar cannot be clicked


I'm trying to learn PyQt5 and currently and making a MenuBar with an Exit function and an Open function. I currently have the MenuBar showing with shortcuts that work, but I cannot click on the MenuBar when I hover over it. Here is my current code:

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QPushButton, QDesktopWidget, QLabel, QFileDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication
from win32api import GetSystemMetrics

CURRENT_VERSION = 0.1

class Example(QMainWindow):

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

        self.initUI()

    def initUI(self):
        self.setWindowTitle('test')

        window_width = GetSystemMetrics(0)
        window_height = GetSystemMetrics(1)

        self.resize(0.6 * window_width, 0.6 * window_height)
        self.center()

        self.setWindowIcon(QIcon('Icon.png'))

        #Exit on menubar
        exitAct = QAction('&Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit applicatiion')
        exitAct.triggered.connect(qApp.quit)

        #Open on menubar
        openAct = QAction('&Open', self)
        openAct.setShortcut('Ctrl+O')
        openAct.setStatusTip('Open Directory')
        openAct.triggered.connect(self.openFile)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)
        fileMenu.addAction(openAct)

        btn = QPushButton("Test", self)
        btn.resize(btn.sizeHint())
        btn.move(50, 50)

        btn.clicked.connect(self.buttonpress)

        self.label = QLabel(self)
        self.image = QLabel(self)
        self.openDirectoryDialog = ""

        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def openFile(self):
        self.openDirectoryDialog=ddir = QFileDialog.getExistingDirectory(self, "Get Dir Path")

    def buttonpress(self):
        label = QLabel(self)
        self.label.move(100,150)
        self.label.setFixedWidth(500)
        self.label.setFixedHeight(100)
        self.label.setText(self.openDirectoryDialog)


if __name__ == '__main__':

    app = QApplication(sys.argv)

    w = Example()

    sys.exit(app.exec_())

It was working before I added the ability to open a file and only had the exit menu item, but I cannot get even that working again.


Solution

  • The problem is caused because the QLabels are above the menu, blocking the click event of this. QMainWindow has a particular structure as shown in the following image:

    enter image description here

    If you want to add a widget, you must do it to the Central Widget, when you pass a parent to a widget, it is placed in position `(0, 0) in relation to it.

    Making the modifications you get the following:

    class Example(QMainWindow):
        [...]
        def initUI(self):
            [...]
    
            #Exit on menubar
            exitAct = QAction('&Exit', self)
            exitAct.setShortcut('Ctrl+Q')
            exitAct.setStatusTip('Exit applicatiion')
            exitAct.triggered.connect(qApp.quit)
    
            #Open on menubar
            openAct = QAction('&Open', self)
            openAct.setShortcut('Ctrl+O')
            openAct.setStatusTip('Open Directory')
            openAct.triggered.connect(self.openFile)
    
            menubar = self.menuBar()
    
            fileMenu = menubar.addMenu('&File')
            fileMenu.addAction(exitAct)
            fileMenu.addAction(openAct)
    
            centralwidget = QWidget(self)
            self.setCentralWidget(centralwidget)
            btn = QPushButton("Test", centralwidget)
            btn.resize(btn.sizeHint())
            btn.move(50, 50)
    
            btn.clicked.connect(self.buttonpress)
    
            self.label = QLabel(centralwidget)
            self.image = QLabel(centralwidget)
            [...]