PyQt 无法识别箭头键 [英] PyQt not recognizing arrow keys

查看:77
本文介绍了PyQt 无法识别箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a (currently very) simple PyQt application, and wanted to allow users to navigate using the arrow keys, rather than clicking buttons.

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

I have the basics implemented, and in my main QWidget, I override keyPressEvent, and right now, all I ask is that it raise an alert (QMessageBox.information(self, "Hey", "pressed:{}".format(event), QMessageBox.Ok)).

这对于标准 ASCII 键(例如字母和 Enter 键)非常有效,但是当我按下箭头键时它完全被忽略了,而且我终生无法弄清楚原因.

This works perfectly well for standard ASCII keys (such as letters and the Enter key), but it is completely ignoring when I press the arrow keys, and I can't for the life of me figure out why.

我的一部分想知道键盘是否还有其他问题,因为我使用的是没有数字键盘的笔记本电脑(它是带有标准常规 ASCII 键盘的 HP,四个箭头键楔在右侧 Shift 下方),并且没有数字键盘).

Part of me wonders if there's some other thing going on with the keyboard since I am on a laptop that does not have a number pad (it's an HP with a standard regular ASCII keyboard, the four arrow keys wedged underneath the right Shift, and no number pad).

有什么建议或帮助吗?如果需要,我可以发布代码,但由于 keyPressEvent 适用于所有其他键,我不确定它会添加什么.

Any suggestions or help? I can post code if needed, but since the keyPressEvent works for every other key, I'm not sure what it would add.

推荐答案

如果你不想关注子部件任何东西.如果您只想要主 QWidget,请设置 Children No Focus Policy;

If your want to don't focus child widget anything. If your want only main QWidget, set Children No Focus Policy;

例如,希望是有帮助的;

Example, hope is helps;

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) 参考

FocusPolicy ENUM 参考

widget 中的当前光标,按键生效.

The current cursor in widget, It have effect with key pressed.

Case 1 : Pure QWidget: 这种情况下它可以跟踪所有的key,因为焦点是self QWidget.

Case 1 : Pure QWidget: This case it can track all key because focus self 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_())

Case 2 : Add button in QWidget : 这种情况无法跟踪箭头键,因为它的焦点按钮和箭头键已被设置新的焦点位置所使用.(如果您在 linux 中使用风格 Windows,您可以看到焦点网格线)

Case 2 : Add button in QWidget : This case can't track arrow key because it focus button and arrow key has used by set new position of focus. (If your use style Windows in linux your can see grid-line of focus)

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中有QtCore.Qt.Key的ENUM,你可以阅读类参考;


I don't know what is ASCII of arrow key return by PyQt, But Your can avoid that. In pyQt have ENUM of QtCore.Qt.Key, your can read class reference;

示例:

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 参考

这篇关于PyQt 无法识别箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆