Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- I want to make 'q' work with and without the menu bar. If you press 'q', the app. should close.
- File -> Quit would do the same.
- """
- import sys
- from PyQt5.QtGui import QKeySequence
- from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QShortcut
- class Window(QMainWindow):
- def __init__(self):
- super().__init__()
- # if you leave this shortcut and press 'q' (twice in my case), you get the following error:
- # QAction::eventFilter: Ambiguous shortcut overload: Q
- # and the shortcut 'q' won't work
- self.shortcutQuit = QShortcut(QKeySequence("q"), self)
- self.shortcutQuit.activated.connect(self.close)
- self.InitWindow()
- def InitWindow(self):
- self.mainMenu = self.menuBar()
- fileMenu = self.mainMenu.addMenu("&File")
- hideItem = QAction("Hide Menu Bar", self)
- hideItem.setShortcut("h")
- hideItem.triggered.connect(self.mainMenu.hide)
- quitItem = QAction("Quit", self)
- quitItem.setShortcut("Q")
- quitItem.triggered.connect(self.close)
- fileMenu.addAction(hideItem)
- fileMenu.addAction(quitItem)
- if __name__ == "__main__":
- App = QApplication(sys.argv)
- window = Window()
- window.show()
- sys.exit(App.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement