ventana una libreria gui ejemplos curso crear con python python-2.7 pyqt pyqt4 keyboard-shortcuts

python - una - libreria pyqt5



Quiero usar atajos de teclado en mi código en PyQt4 (1)

Estoy desarrollando un programa de administración de habitación. Cuando el usuario presiona la tecla "ESC", se termina "Dialog". Quiero prevenir esto Entonces, quiero usar ''segundo código'' en ''primer código''

from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def setupUi(self, Dialog): self.Dialog = Dialog self.Dialog.setObjectName(_fromUtf8("self.Dialog")) self.Dialog.resize(190, 98) self.pushButton = QtGui.QPushButton(self.Dialog) self.pushButton.setGeometry(QtCore.QRect(0, 0, 191, 101)) font = QtGui.QFont() font.setPointSize(13) self.pushButton.setFont(font) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.retranslateUi(self.Dialog) QtCore.QMetaObject.connectSlotsByName(self.Dialog) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(''released()''), self.Dialog.close) # <- put signal to close when clicked. def retranslateUi(self, Dialog): self.Dialog.setWindowTitle(_translate("self.Dialog", "self.Dialog", None)) self.pushButton.setText(_translate("self.Dialog", "hi", None)) class QCustomDialog (QtGui.QDialog): # <- Implement your own def closeEvent(self, event): reply = QtGui.QMessageBox.question(self, ''Message'', "Are you sure to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: event.accept() else: event.ignore() if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Dialog = QCustomDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())

from PyQt4.Qt import Qt from PyQt4 import QtCore, QtGui from PyQt4.QtCore import QObject, SIGNAL from PyQt4.QtGui import QWidget, QApplication, QMainWindow, QAction, QIcon, QKeySequence import os, time, MySQLdb, socket, sys class MainWindow(QMainWindow): def __init__(self, parent): QMainWindow.__init__(self, parent) self.centralwidget = QWidget(self) self.action = QAction(QIcon(), "Down", self) self.action.setShortcut("ESC") self.action.setShortcutContext(Qt.ApplicationShortcut) self.addAction(self.action) QObject.connect(self.action, SIGNAL("triggered()"), self.down) def down(self): print ''DOWN!!!'' def main(): app = QApplication(sys.argv) mw = MainWindow(None) mw.show() sys.exit(app.exec_()) if __name__ == ''__main__'': main()


Una forma fácil de usar el atajo de teclado es usar QShortcut usando la secuencia de teclas en 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

Clase QShortcut : http://pyqt.sourceforge.net/Docs/PyQt4/qshortcut.html

Clase QKeySequence : http://pyqt.sourceforge.net/Docs/PyQt4/qkeysequence.html

Referencia Qt.Key : http://pyqt.sourceforge.net/Docs/PyQt4/qt.html

Otra forma de implementar el código anterior, este ejemplo muestra cómo implementar en el diálogo:

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)