problems with getting keypress event
-
wrote on 25 Nov 2021, 14:09 last edited by
Hi,
here's my actual question (before I was shocked by Qt's stylesheet handling)
I have these widgets:
The green area is a notebook-page, which manages 10 (child-)editors. Each editor has lineedits.
All but one editors are disabled and an editor can be selected by CTRL+numberThe notebook-widget is part of a widget stack, so not always visible. That was the reason for me, not to use default tab shortcuts but instead overload
keypressEvent
.
I can use ALT+number to change current tab.Now when the editor-page is active, key-events go into lineedit. Then the editor gets a keypressEvent and the container widget, that holds all editors. But after that widget, key-events are routed to mainwindow.
I found no chance for the notebook widget, to see those keystrokes. I wanted to pass keypressEvent to parent of container widget, but keypressEvent is protected. (Between notebook and editor-manager there are several internal widgets like scrollarea, internal widget, ...
Passing key-event to widgets parent does not work and I guess, same is true, if I wonna route the key-event from mainwindow to center widget (the widget stack).
Any hints, how the notebook could see those key-events?
-
Hi,
You can use an event filter.
-
wrote on 26 Nov 2021, 12:57 last edited by
Thank you for the pointer!
I had completely forgotten about that possibility. I used it in my first prototypes - long time ago :/
... in this case it did not help to get the desired result. Too many widgets between notebook and lineedit.
But it was very helpful any way!It bothered me that even with Ctrl or Alt modifiers the number-/letter-keys produce an input in the lineedit.
With the event filter I was able to iron out the flawed concept.First I did not like that I now have to code the access keys of the notebook in the editor. But after a short thought I realized that actually every input field needs an EventFilter to not process number and letter keys with modifiers as input.
This kind of workaround is ok for me now ;)
It works like this:
- editor adds itself as eventfilter to lineedit
- editor checks for modifiers at keypress
- if a modifier is active, editor calls
keyPressEvent
followed by return true - without modifiers active eventFilter returns false, which leads to default key processing of lineedit. Those keys don't arrive atkeyPressEvent
.
keyPressEvent
routes keys that will not be processed to its superclass. This will route key events toQMainWindow
(I checked that by following timecode of event).
As its not possible to route events to widgets parents, it was possible to distribute events top down, as all classes are my classes.So I changed
keyPressEvent
to public.
After that change,QMainWindow
passes unprocessed key events to center widget (which in my case is a stacked layout). As I manage all stacked pages with an individual class, it was easy to changekeyPressEvent
to public too.
The stacked widget manager then distributes key events to the active page (widget), which in this case is the notebook.It's a big detour until the event reaches its destination, but it works.
1/3