I'm writing a small pyqt program. I want the main window to to react to arrow movement. I added an event to my MainGui class, keyPressEvent, that handle this. The event work fine as long as I don't press certain buttons such as Key_Up or Key_Down are directed to my (currently only) QComboBox and not to my mainGui. I tried to give the focus to mainGui after each paintEvent but then I need to double click on buttons/comboBox.
Then I tried to use the MousePressEvent to check if a certain element is under the mouse. This work fine with the comboBox, but not with the button.
So, how can I direct key events to the mainGui or give the focus to QButtons?
I used
eventFilterto identify when the mouse enter theQPushButtonand give it focus:keepFocusis a flag I initialized in the__init__function of the class. I added this part at thepaintEventfunctionNow, I keep the focus at the
MainGuiand I only give it to the button when the mouse hove over it. If I do another action (like pressing a mouse button or a keyboard key) the focus is given back to theMainGui. This will create some buggy filling (For example, I need to press twice a keyboard key before the first response) but this is workable.