Solved [MacOS] Global QMenuBar is not clickable
-
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() and QMainWIndow.menubar()
However created global menubar is not clickable. I clicked it but it does not show submenus below 'File' Menu.
But This way below works fine like Windows Applications but I wanna use native MacOs menubar
QMainWindow.setMenuBar() and QMainWindow.setNativeMenuBar(False)
Here are codes and images.
import sys from PyQt5.QtWidgets import QApplication, QAction, QMenuBar from PyQt5.QtGui import QKeySequence from PyQt5.QtCore import pyqtSlot, pyqtSignal 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): act_new_file = QAction(self.tr("&File"), self) act_new_file.setShortcut(QKeySequence('Ctrl+N')) 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_open_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(self.tr('&File')) self.menu_file.addAction(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__': # Some Test codes. import sys from PyQt5.QtWidgets import QMainWindow from menuBar import MenuBarWidget class Form(QMainWindow): 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_())
-
@SGaist I solved it by myself. It was unfocus and refocus issue in macos
Same issue here.
https://forum.qt.io/topic/79478/menubar-does-not-work-until-app-unfocused-and-focused-again-on-macosx/2Method Here.
https://stackoverflow.com/questions/48738805/mac-pyqt5-menubar-not-active-until-unfocusing-refocusing-the-appit works perfect.
-
Hi and welcome to devnet,
Which version of Qt do you have installed along PyQt5 ?
-
@SGaist Thank you for your efforts to improve QT community first of all !
My versions are [MacOs 10.15.6 and PyQt 5.15 ]
Is this version issue? -
You can test an older version to see if its related to 5.15.
However, this rings a bell. Can you test that with PySide2 ?
Once tested with PySide2, you should check the bug report system to see if this is something known.
-
@SGaist I have tried PyQt5 5.14.2 , 5.13.2 version and Pyside2. None of them works and makes it clickable.
I guess I coded wrong? or latest macos version problem? MenuBarWidget inherits from QMenuBar(), then returns an custom menu instance to QMainWindow.setMenuBar.setMenuBar(). I can see File menu is created on the native macos menubar BUT IT'S NOT CLICKABLE and DOES NOT SHOW SUBMENUS . lol -
@SGaist I solved it by myself. It was unfocus and refocus issue in macos
Same issue here.
https://forum.qt.io/topic/79478/menubar-does-not-work-until-app-unfocused-and-focused-again-on-macosx/2Method Here.
https://stackoverflow.com/questions/48738805/mac-pyqt5-menubar-not-active-until-unfocusing-refocusing-the-appit works perfect.
-
You still shouldn't need a workaround. I'd say it's worth a report since it has been working until now.