i want to use keyboard-shortcuts in my code in pyqt4

  • Last Update :
  • Techknowledgy :

Easy way to use keyboard shortcut is to use QShortcut by using the key sequence in QKeySequence:

class MainWindow(QtGui.QMainWindow):
   def __init__(self, parent = None):
   QtGui.QMainWindow.__init__(self, parent)
   .
   .
   .
self.myQCustomDialog = QCustomDialog() # < -From code 1
ui = Ui_Dialog() # < -From code 1
ui.setupUi(self.myQCustomDialog) # < -From code 1
self.setCentralWidget(self.myQCustomDialog) # < -Set to this central widget
   .
   .
   .
self.connect(QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self), QtCore.SIGNAL('activated()'), self.down)

def down(self):
   print 'DOWN!!!'
# Or put code to implement from code 1

Another way implement the code above, this example shows how to implement in dialog:

class Ui_Dialog(object):
   def setupUi(self, Dialog):
   .
   .
   .
QtCore.QObject.connect(QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self.Dialog), QtCore.SIGNAL('activated()'), self.Dialog.close)

Suggestion : 2

last modified July 9, 2020

1._
#!/usr/bin/python


from PyQt5.QtWidgets
import QWidget, QShortcut, QApplication, QMessageBox
from PyQt5.QtGui
import QKeySequence
import sys

class Example(QWidget):

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

self.initUI()

def initUI(self):

   self.msgSc = QShortcut(QKeySequence('Ctrl+M'), self)

self.msgSc.activated.connect(lambda: QMessageBox.information(self,
   'Message', 'Ctrl + M initiated'))

self.quitSc = QShortcut(QKeySequence('Ctrl+Q'), self)
self.quitSc.activated.connect(QApplication.instance().quit)

self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Shortcuts')
self.show()

def main():

   app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

if __name__ == '__main__':
   main()

The Ctrl + M shortcut shows a message box and the Ctrl + Q quits the application.

self.msgSc = QShortcut(QKeySequence('Ctrl+M'), self)

The Ctrl + M shortcut is created. It is applied to the main window widget.

self.msgSc.activated.connect(lambda: QMessageBox.information(self,
   'Message', 'Ctrl + M initiated'))
5._
#!/usr/bin/python


from PyQt5.QtWidgets import(QWidget, QHBoxLayout, QApplication,
   QPushButton, QMessageBox, QSizePolicy)
from PyQt5.QtGui
import QKeySequence
from PyQt5.QtCore
import Qt
import sys

class Example(QWidget):

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

self.initUI()

def initUI(self):

   hbox = QHBoxLayout()

msgBtn = QPushButton('&Show message', self)
msgBtn.clicked.connect(lambda: QMessageBox.information(self,
   'Message', 'Information message'))

hbox.addWidget(msgBtn)
msgBtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
hbox.setAlignment(Qt.AlignLeft)

self.setLayout(hbox)

self.move(300, 300)
self.setWindowTitle('Shortcuts')
self.show()

def main():

   app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

if __name__ == '__main__':
   main()

A message box is displayed when pressing the Alt + S shortcut.

msgBtn = QPushButton('&Show message', self)

Suggestion : 3

Jan 14, 2020 | PyQt5, Python | 0 comments

Source Code:

import sys
from PyQt5.QtGui
import QKeySequence
from PyQt5.QtWidgets
import QApplication, QWidget, QShortcut, QLabel, QHBoxLayout

class AppDemo(QWidget):
   def __init__(self):
   super().__init__()

self.label = QLabel('Press Ctrl + O', self)

self.shortcut_open = QShortcut(QKeySequence('Ctrl+O'), self)
self.shortcut_open.activated.connect(self.on_open)

self.shortcut_close = QShortcut(QKeySequence('Ctrl+Q'), self)
self.shortcut_close.activated.connect(self.closeApp) # or lambda: app.quit()

self.layout = QHBoxLayout()
self.layout.addWidget(self.label)

self.setLayout(self.layout)
self.resize(150, 150)

def on_open(self):
   print('Ctrl O has been fired')

def closeApp(self):
   app.quit()

app = QApplication(sys.argv)

demo = AppDemo()
demo.show()

sys.exit(app.exec_())

Suggestion : 4

The QShortcut class provides a way of connecting keyboard shortcuts to Qt’s signals and slots mechanism, so that objects can be informed when a shortcut is executed. The shortcut can be set up to contain all the key presses necessary to describe a keyboard shortcut, including the states of modifier keys such as Shift, Ctrl, and Alt.,Constructs a QShortcut object for the parent widget. Since no shortcut key sequence is specified, the shortcut will not emit any signals.,For applications that use menus, it may be more convenient to use the convenience functions provided in the QMenu class to assign keyboard shortcuts to menu items as they are created. Alternatively, shortcuts may be associated with other types of actions in the QAction class.,The QShortcut class is used to create keyboard shortcuts. More…

shortcut = QShortcut(QKeySequence(self.tr("Ctrl+O", "File|Open")),
   parent)
setKey(0) # no signal emitted
setKey(QKeySequence()) # no signal emitted
setKey(0x3b1) # Greek letter alpha
setKey(Qt.Key_D) # 'd', e.g.to delete
setKey('q') # 'q', e.g.to quit
setKey(Qt.CTRL + Qt.Key_P) # Ctrl + P, e.g.to print document
setKey("Ctrl+P") # Ctrl + P, e.g.to print document

Suggestion : 5

Last Updated : 19 Jul, 2022

Output:

Hotkey Detected