1

First off -- thanks for this group! I started delving into PyQt a month or so ago. In that time, I've bumped up against many questions, and virtually always found an answer here.

Until now.

I have a workaround for this, but I think it's a kluge and there probably is a proper way. I'd like to understand better what's going on.

Here's the code:

from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class FormWidget(QWidget): def __init__(self, parent): super(FormWidget, self).__init__(parent) # Create view with image in it self.image = QGraphicsPixmapItem(QPixmap()) self.scene = QGraphicsScene() self.scene.addItem(self.image) self.view = QGraphicsView(self.scene) self.hlayout = QHBoxLayout() self.hlayout.addWidget(self.view) self.setLayout(self.hlayout) # self.view.keyPressEvent = self.keyPressEvent def keyPressEvent(self, event): key = event.key() mod = int(event.modifiers()) print( "<{}> Key 0x{:x}/{}/ {} {} {}".format( self, key, event.text(), " [+shift]" if event.modifiers() & Qt.SHIFT else "", " [+ctrl]" if event.modifiers() & Qt.CTRL else "", " [+alt]" if event.modifiers() & Qt.ALT else "" ) ) class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) form = FormWidget(self) self.setCentralWidget(form) if __name__ == '__main__': import sys app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_()) 

As is, all keyboard input is detected by the overloaded keyPressEvent() function, except arrow keys. I've found enough posts talking about this to have a sense that it is because the child widget (self.view) is receiving them. I presume the child widget is, in fact, receiving all the keystrokes, but ignoring the ones that are getting through, and sucking up the arrow keys, which is why they aren't getting to the parent's keyPressEvent() function. That seems to be so, because if I uncomment the line in the middle:

 self.view.keyPressEvent = self.keyPressEvent 

It behaves as I expect -- the parent's keyPressEvent() gets all the keystrokes, arrows included.

How would I tell the child widget to ignore all keystrokes? I thought maybe this:

 self.view.setFocusPolicy(Qt.NoFocus) 

When I add that, keyPressEvent() doesn't see any keystrokes at all.

I suppose I could overload keyPressEvent() for the child as well, and just explicitly pass everything up to the parent. But that doesn't seem better than my kluge.

I think I must be misunderstanding something here.

Thanks. Just looking to learn ...

2
  • QKeySequences require a defined focus policy for the widget (see the Qt docs here): doc.qt.io/qt-4.8/qwidget.html#keyPressEvent The simplest solution is likely to reimplement an eventFilter, as posted below, although I'm somewhat surprised that it is not handling arrow keys. Commented Jan 27, 2016 at 19:56
  • Based on this, I tried self.setFocusPolicy(Qt.StrongFocus) on the parent [rather than self.view.setFocusPolicy(Qt.NoFocus) on the child]. That seems to make it behave the way I expect. It may still be the case that reimplementing eventFilter is a more appropriate way to go. But now I understand better, I think. Thanks all. Commented Jan 27, 2016 at 21:10

2 Answers 2

3

By default, a QWidget does not accept the keyboard focus, so you need to enable it explicitly:

class FormWidget(QWidget): def __init__(self, parent): ... self.setFocusPolicy(Qt.StrongFocus) 
Sign up to request clarification or add additional context in comments.

Comments

2

Rather than subclassing the child widget or attempting to prevent keystrokes from reaching it, you should consider using an eventFilter to capture events on the child widget. You will see all events before the child widget, and you can suppress or transform them.

http://doc.qt.io/qt-5.5/qobject.html#eventFilter

1 Comment

In fact, I'd seen posts suggesting using eventFilter. In one version of my code, I implemented it that way, and that did work. I was just interested in understanding better why it wasn't working the way I posted. Thanks for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.