Handle arrow key events by setting the focus policy

懵懂的女人 提交于 2020-01-14 04:43:06

问题


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.

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

回答1:


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...



来源:https://stackoverflow.com/questions/39807858/handle-arrow-key-events-by-setting-the-focus-policy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!