PyQt无法识别箭头键

时间:2014-08-22 02:12:05

标签: python pyqt4 arrow-keys

我正在尝试编写一个(当前非常)简单的PyQt应用程序,并希望允许用户使用箭头键进行导航,而不是单击按钮。

我已经实现了基础知识,并且在我的主QWidget中,我覆盖了keyPressEvent,而现在,我要求的是它会发出警报(QMessageBox.information(self, "Hey", "pressed:{}".format(event), QMessageBox.Ok))。

这对于标准的ASCII键(例如字母和Enter键)非常有效,但是当我按下箭头键时它完全忽略了,我不能为我的生活找出原因。

我的一部分想知道键盘上是否还有其他事情,因为我在没有数字键盘的笔记本电脑上(这是带有标准常规ASCII键盘的HP,四个箭头键楔在右侧Shift下方,而且没有数字垫)。

有任何建议或帮助吗?我可以根据需要发布代码,但由于keyPressEvent适用于其他所有密钥,我不确定它会添加什么。

1 个答案:

答案 0 :(得分:5)

如果您不想将儿童小部件集中在任何地方。如果您只想要主QWidget,请设置儿童无焦点政策;

例如,希望是有帮助的;

import sys
from PyQt4 import QtGui, QtCore

class QCustomWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        super(QCustomWidget, self).__init__(parent)
        myQLayout = QtGui.QVBoxLayout()
        self.my1QPushButton = QtGui.QPushButton('Test 1', self)
        self.my2QPushButton = QtGui.QPushButton('Test 2', self)
        self.setChildrenFocusPolicy(QtCore.Qt.NoFocus)
        myQLayout.addWidget(self.my1QPushButton)
        myQLayout.addWidget(self.my2QPushButton)
        self.setLayout(myQLayout)

    def setChildrenFocusPolicy (self, policy):
        def recursiveSetChildFocusPolicy (parentQWidget):
            for childQWidget in parentQWidget.findChildren(QtGui.QWidget):
                childQWidget.setFocusPolicy(policy)
                recursiveSetChildFocusPolicy(childQWidget)
        recursiveSetChildFocusPolicy(self)

    def keyPressEvent (self, eventQKeyEvent):
        messageQMessageBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, 'Question', 'Hello Main', QtGui.QMessageBox.Yes)
        messageQMessageBox.exec_()
        QtGui.QWidget.keyPressEvent(self, eventQKeyEvent)

appQApplication = QtGui.QApplication(sys.argv)
windowQCustomWidget = QCustomWidget()
windowQCustomWidget.setFixedSize(640, 480)
windowQCustomWidget.show()
sys.exit(appQApplication.exec_())

QWidget.setFocusPolicy (self, Qt.FocusPolicy policy) Reference

FocusPolicy ENUM Reference


小部件中的当前光标,按下按键有效。

案例1:纯QWidget:这种情况下,它可以跟踪所有密钥,因为焦点自我QWidget

import sys
from PyQt4 import QtGui, QtCore

class QCustomWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        super(QCustomWidget, self).__init__(parent)
        myQLayout = QtGui.QVBoxLayout()
        self.setLayout(myQLayout)

    def keyPressEvent (self, eventQKeyEvent):
        messageQMessageBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, 'Question', 'Hello', QtGui.QMessageBox.Yes)
        messageQMessageBox.exec_()
        QtGui.QWidget.keyPressEvent(self, eventQKeyEvent)

appQApplication = QtGui.QApplication(sys.argv)
windowQCustomWidget = QCustomWidget()
windowQCustomWidget.setFixedSize(640, 480)
windowQCustomWidget.show()
sys.exit(appQApplication.exec_())

案例2:QWidget中的添加按钮:此案例无法跟踪箭头键,因为焦点按钮和箭头键已设置新的焦点位置。 (如果你在Linux中使用Windows样式,你可以看到网格线的重点)

import sys
from PyQt4 import QtGui, QtCore

class QCustomWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        super(QCustomWidget, self).__init__(parent)
        myQLayout = QtGui.QVBoxLayout()
        self.my1QPushButton = QtGui.QPushButton('Test 1', self)
        self.my2QPushButton = QtGui.QPushButton('Test 2', self)
        myQLayout.addWidget(self.my1QPushButton)
        myQLayout.addWidget(self.my2QPushButton)
        self.my1QPushButton.keyPressEvent = self.button1KeyPressEvent
        self.my2QPushButton.keyPressEvent = self.button2KeyPressEvent
        self.setLayout(myQLayout)

    def keyPressEvent (self, eventQKeyEvent):
        messageQMessageBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, 'Question', 'Hello Main', QtGui.QMessageBox.Yes)
        messageQMessageBox.exec_()
        QtGui.QWidget.keyPressEvent(self, eventQKeyEvent)

    def button1KeyPressEvent (self, eventQKeyEvent):
        messageQMessageBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, 'Question', 'Hello Button 1', QtGui.QMessageBox.Yes)
        messageQMessageBox.exec_()
        QtGui.QPushButton.keyPressEvent(self.my1QPushButton, eventQKeyEvent)

    def button2KeyPressEvent (self, eventQKeyEvent):
        messageQMessageBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, 'Question', 'Hello Button 2', QtGui.QMessageBox.Yes)
        messageQMessageBox.exec_()
        QtGui.QPushButton.keyPressEvent(self.my2QPushButton, eventQKeyEvent)

appQApplication = QtGui.QApplication(sys.argv)
windowQCustomWidget = QCustomWidget()
windowQCustomWidget.setFixedSize(640, 480)
windowQCustomWidget.show()
sys.exit(appQApplication.exec_())

我不知道PyQt返回箭头键的ASCII是什么,但你可以避免这种情况。在pyQt中,ENUM为QtCore.Qt.Key,您可以读取类引用;

示例:

def keyPressEvent (self, eventQKeyEvent):
    key = eventQKeyEvent.key()
    if key == QtCore.Qt.Key_F1:
        print 'Help'
    elif key == QtCore.Qt.Key_F5:
        print 'Reload'
    elif key == QtCore.Qt.Key_Left:
        print 'Left'
    elif key == QtCore.Qt.Key_Up:
        print 'Up'
    elif key == QtCore.Qt.Key_Right:
        print 'Right'
    elif key == QtCore.Qt.Key_Down:
        print 'Down'

另请参阅:Qt.Key ENUM Reference