PDA

View Full Version : Cannot capture Key_Up



xenome
8th September 2009, 23:00
Hi,

I have a QWidget subclass (call it Foo) that holds a child GraphicsView and GraphicsScene. I've added Foo as the central widget to a MainWindow object and cannot capture the key press event for Key_Up. I have a feeling this has to do with focus handling as I see all other key strokes. I've sniffed all the events to the Foo widget and can see QEvent::ShortcutOverride when the Up arrow is depressed and KeyRelease when the key is released.

The keyPressEvent() works perfectly fine in the GraphicsView.

Any thoughts on how to enable this?

wysota
9th September 2009, 09:17
You have to set the focus policy and optionally focus proxy for your widgets appropriately.

xenome
9th September 2009, 19:36
Thanks for the reply. I played around with the focus proxy and policy but still cannot get it to work.

Do I call MainWindow->setFocusProxy(Foo Widget)? and MainWindow->setFocusPolicy(Qt::NoFocus) ?

Leaving everything else alone on the Foo Widget. My Foo Widget children (GraphicsView/Scene) are still receiving the event correctly.

wysota
9th September 2009, 20:51
What's the focus policy of the widget you want to capture the key in?

xenome
10th September 2009, 01:55
Everything is set to default.

I'm assuming the MainWindow and centralwidget() configuration do something to intercept the Up/Down Keys.

I figured that's why I get the QEvent::ShortcutOverride event when I press the Up/Down key while sniffing the Foo widget's events.

wysota
10th September 2009, 07:59
Everything is set to default.

I'm assuming the MainWindow and centralwidget() configuration do something to intercept the Up/Down Keys.

Default focus policy of QWidget (and QMainWindow as well) is NoFocus.

Your widget simply doesn't have focus so it won't receive key events. If you want something to happen on a specific key combination you can register a shortcut for that combination - this is done via QShortcut (of course you still won't receive a keyPressEvent in your widget).

xenome
10th September 2009, 15:45
Well I would think it has some focus as I receive all key events except the arrows.

wysota
10th September 2009, 16:21
Please provide a minimal compilable example reproducing the problem.