Reputation: 1
There were something wrong when I was using PyQt5 to build a GUI window with a menu bar.
Here is my code:
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
bar = self.menuBar()
example1 = QAction('Exit', self)
example1.setShortcut('Ctrl+E')
example1.triggered.connect(self.close)
example2 = QAction('xit', self)
example2.setShortcut('Ctrl+A')
example2.triggered.connect(self.close)
example3 = QAction('Quit', self)
example3.setShortcut('Ctrl+Q')
example3.triggered.connect(self.close)
fileMenu = bar.addMenu('File')
fileMenu.addAction('NNN')
fileMenu.addAction(example1)
fileMenu.addAction(example2)
fileMenu.addAction(example3)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Menu Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
When I run this, the menu bar looks like this:
As the picture shows, 'Exit' and 'Quit' disappeared, but the shortcut worked.
My Env: Python 3.6.5, PyQt 5.11.1, MAC_OS 10.13.5
Upvotes: 0
Views: 668
Reputation: 260
The qt site says the following
Note: Do not call QMainWindow::menuBar() to create the shared menu bar, because that menu bar will have the QMainWindow as its parent. That menu bar would only be displayed for the parent QMainWindow.
Try to change bar = self.menuBar()
to bar = QtGui.MenuBar()
see reference form here
Upvotes: 1