Advertisement
Guest User

Untitled

a guest
May 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. I want to make 'q' work with and without the menu bar. If you press 'q', the app. should close.
  5. File -> Quit would do the same.
  6. """
  7.  
  8. import sys
  9.  
  10. from PyQt5.QtGui import QKeySequence
  11. from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QShortcut
  12.  
  13.  
  14. class Window(QMainWindow):
  15.     def __init__(self):
  16.         super().__init__()
  17.  
  18.         # if you leave this shortcut and press 'q' (twice in my case), you get the following error:
  19.         # QAction::eventFilter: Ambiguous shortcut overload: Q
  20.         # and the shortcut 'q' won't work
  21.         self.shortcutQuit = QShortcut(QKeySequence("q"), self)
  22.         self.shortcutQuit.activated.connect(self.close)
  23.  
  24.         self.InitWindow()
  25.  
  26.     def InitWindow(self):
  27.         self.mainMenu = self.menuBar()
  28.         fileMenu = self.mainMenu.addMenu("&File")
  29.  
  30.         hideItem = QAction("Hide Menu Bar", self)
  31.         hideItem.setShortcut("h")
  32.         hideItem.triggered.connect(self.mainMenu.hide)
  33.  
  34.         quitItem = QAction("Quit", self)
  35.         quitItem.setShortcut("Q")
  36.         quitItem.triggered.connect(self.close)
  37.  
  38.         fileMenu.addAction(hideItem)
  39.         fileMenu.addAction(quitItem)
  40.  
  41. if __name__ == "__main__":
  42.     App = QApplication(sys.argv)
  43.     window = Window()
  44.     window.show()
  45.     sys.exit(App.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement