PyQt5 on MacOs. Custom QMenuBar added to QMainWindow is not clickable

502 views Asked by At
class MenuBarWidget(QMenuBar):
    sig_new_file = pyqtSignal()
    sig_open_file = pyqtSignal()
    sig_save_file = pyqtSignal()
    sig_page_setup = pyqtSignal()
    sig_print = pyqtSignal()
    sig_exit = pyqtSignal()
    sig_status_bar = pyqtSignal(bool)

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

    # self.init_menu_edit()
    # self.init_menu_format()
    # self.init_menu_view()
    # self.init_menu_help()
    # self.set_all_text()

def init_menu_file(self):
    self.act_new_file = QAction('File', self)
    self.act_new_file.setShortcut(QKeySequence('Ctrl+N'))
    self.act_new_file.triggered.connect(lambda: self.sig_new_file.emit())

    self.act_open_file = QAction('Open', self)
    self.act_open_file.setShortcut(QKeySequence('Ctrl+O'))
    self.act_new_file.triggered.connect(lambda: self.sig_open_file.emit())

    self.act_save_file = QAction('Save', self)
    self.act_save_file.setShortcut(QKeySequence('Ctrl+S'))
    self.act_save_file.triggered.connect(lambda: self.sig_save_file.emit())

    self.act_print = QAction('Print', self)
    self.act_print.setShortcut(QKeySequence('Ctrl+P'))
    self.act_print.triggered.connect(lambda: self.sig_print.emit())

    self.act_quit = QAction('Quit', self)
    self.act_quit.setShortcut(QKeySequence('Ctrl+Q'))
    self.act_quit.triggered.connect(lambda: self.sig_exit.emit())

    self.menu_file = self.addMenu('&File')
    self.menu_file.addAction(self.act_new_file)
    self.menu_file.addAction(self.act_open_file)
    self.menu_file.addAction(self.act_save_file)
    self.menu_file.addSeparator()
    self.menu_file.addAction(self.act_print)
    self.menu_file.addSeparator()
    self.menu_file.addAction(self.act_quit)


if __name__ == '__main__':

class Form(QMainWindow):

    sig_new_file = pyqtSignal()

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

    def initUI(self):
        self.resize(640, 480)
        self.setWindowTitle('MenuBar')
        self.menu = MenuBarWidget()
        self.setMenuBar(self.menu)

app = QApplication(sys.argv)
form = Form()
form.show()
exit(app.exec_())

I wanted to make a menubar for a window on MacOs 10.15.6 with PyQt 5.15

I used QMenuBar class for custom menubar ,created some submenus under 'File' Menu and added it to QMainWindow and I can see global menubar is created on the top of display just like the other MacOs.

I tried both

QMenuBar()  , QMainWIndow.setMenuBar()
sub_menu = QAction('Someting else..',self)

fileMenu = QMainWindow.menuBar().addMenu('File')
fileMenu.addAction(sub_menu)

However created global menubar is not clickable. I clicked it but it does not show submenus below 'File' Menu.

But This way using 'setNativeMenuBar(False)' below works fine like Windows Applications but I wanna use native MacOs menubar

QMainWindow.menuBar() and QMainWindow.setNativeMenuBar(False)

Here are images.

https://i.stack.imgur.com/hv0VA.jpg

QMainWindow and MacOS universial menubar

2

There are 2 answers

0
GrammyPuter On BEST ANSWER
# This worked on PyQt5 on macOS Big Sur (ver 11.6)
#    it is the result on painful trial and error
#    and reading the Qt Docs

    def _createMenuBar(self):
        menuBar = self.menuBar()

        # File menu
        fileMenu = menuBar.addMenu("File")
        fileMenu.addAction("New")

        save = QAction("Save",self)
        save.setShortcut("Ctrl+S")
        fileMenu.addAction(save)

        quit = QAction("\0Quit",self)
        quit.setShortcut("Ctrl+Q")
        fileMenu.addAction(quit)

        # Edit menu
        editMenu = menuBar.addMenu("Edit")
        editMenu.addAction("Copy")
        editMenu.addAction("Paste")

        # Help menu
        helpMenu=menuBar.addMenu("Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)
0
chidalu okechukwu On

This might help from PyQt5.QtWidgets import QMenuBar

Mainmenu=self.menuBar();# creates a menu bar widget

Filemenu=Mainmenu.addMenu('file');#this adds file to menubar

Editmenu=Mainmenu.addMenu('Edit');#this adds edit to menubar