python - 我想在 PyQt4 的代码中使用键盘快捷键

标签 python python-2.7 pyqt pyqt4 keyboard-shortcuts

我正在开发一个 p-room 管理程序。当用户按下“ESC”键时,“Dialog”终止。我想阻止这种情况。所以,我想在“第一个代码”中使用“第二个代码”

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()

最佳答案

使用键盘快捷键的简单方法是使用 QShortcut 通过使用 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

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

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

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


另一种方式实现上面的代码,这个例子展示了如何在对话框中实现:

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)

关于python - 我想在 PyQt4 的代码中使用键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25398528/

相关文章:

python - os.listdir 模拟压缩目录

python - 将对象添加到 SQLAlchemy 关联对象时出现 KeyError

python - 有效同目录导入时出现 PyCharm 错误

python - 使用 Python 将具有参数限制的积分函数拟合到数据(德拜模型)

python - 初始化一个列表并找到该列表的平均值

python QLineEdit 文本颜色

python - 如何通过拖动使 PyQt 小部件可调整大小?

python - 如何使用 QSortFilterProxyModel 对浮点值进行排序?

python - 为什么每行后面都打印 None ?

python - 如何在Linux中启用libattr frature(简称xattr)?