通过设置焦点策略来处理箭头键事件 [英] Handle arrow key events by setting the focus policy

查看:114
本文介绍了通过设置焦点策略来处理箭头键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理我的应用程序中箭头键的键事件.我已经读过,这样做必须禁用焦点.我采用以下方法: PyQt无法识别箭头键.实际上,当在MyApp.__init__中调用self.setChildrenFocusPolicy(QtCore.Qt.NoFocus)(在链接的线程和下面的源代码中定义)时,按下箭头键会引发键事件.但是,我不想在应用程序的整个运行过程中都保持禁用焦点,而只是单击按钮时.因此,将self.setChildrenFocusPolicy(QtCore.Qt.NoFocus)移至按钮单击功能:

I want to handle key events of the arrow keys in my application. I have already read that for doing so the focus must be disabled. I follow this method: PyQt not recognizing arrow keys. Indeed, when calling self.setChildrenFocusPolicy(QtCore.Qt.NoFocus) (as defined in the linked thread and in my source code below) within MyApp.__init__, hitting an arrow key raises a key event. However, I do not want to keep the focus disabled during the entire runtime of the application but only upon clicking a button. So I move self.setChildrenFocusPolicy(QtCore.Qt.NoFocus) to the button click function:

def __init__(self):
    self.pushButton.clicked.connect(self.pushButtonClicked)

def pushButtonClicked(self):
    self.setChildrenFocusPolicy(QtCore.Qt.NoFocus)

实际上,按下按钮将禁用焦点(例如,行编辑不能再使用文本光标了).但是,按箭头键仍不会引发按键事件.

Indeed, hitting the push button disables the focus (e.g. a line edit cannot take the text cursor anymore). However, hitting an arrow key still does not raise a key event.

整个应用程序(您将需要一个带按钮的mainwindow.ui):

The whole application (you will need a mainwindow.ui with a push button):

import sys
from PyQt4 import QtCore, QtGui, uic

qtCreatorFile = "d:/test/mainwindow.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtGui.QMainWindow, Ui_MainWindow):

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

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.pushButtonClicked)

    def pushButtonClicked(self):
        self.setChildrenFocusPolicy(QtCore.Qt.NoFocus)

    def keyPressEvent(self, event):
        print "a"

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

推荐答案

无需禁用焦点.您可以通过在应用程序上安装事件过滤器来访问所有关键事件:

There's no need to disable focus. You can get access to all key events by installing an event filter on the application:

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        ...
        QtGui.qApp.installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.KeyPress:
            print(event.key())
        return super(MyApp, self).eventFilter(source, event)

但是请注意,这确实可以得到一切,因此您最终可能会得到比讨价还价更多的东西...

But note that this really does get everything, so you may end up with more than you bargained for...

这篇关于通过设置焦点策略来处理箭头键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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