PDA

View Full Version : QShortcut not working due to focus problem - even on top window



fispy
13th October 2016, 15:10
I have a Qt application that runs on a linux (Arch Linux, very recent) with PyQt 5.7. The device the application runs on may or may not have a keyboard, may or may not have a touchscreen and may or may not have both. Therefore I created a widget that contains multiple buttons where every button represents a possible action that the user may trigger. In order to provide users without a touchscreen the possibility to trigger the corresponding buttons, I created a QShortcut, added it to the toplevel window (MainWindow) by passing the main window as the parent to the shortcut on construction and connected the activated signal to the click slot of the button.



button = QPushButton (...)
key = self.nr_items () # some integer
button.setText ('[' + str (key) + ']' + button.text ())
toplevelWidget = ... # the MainWindow instance
...
shortcut = QShortcut (QKeySequence (key + Qt.Key_0), toplevelWidget)
shortcut.activated.connect (button.click)


The idea is to have the numbers 1..n as the key sequence (okay, it's only one key not a sequence) to trigger the corresponding button.

The problem now is that the keys simply do not work. If I click (with the touchscreen) into the widget where the buttons are placed, the keys work and trigger the button but if I don't first click into the widget (nothing gets triggered or selected, I guess it only receives the input focus) pressing any corresponding key has no effect. I tried a similar minimal example in C++ and there the same approach (okay, not exactly the same setup, just similar) works flawlessly.

I tried to set the focus on the widget where the buttons are placed, no success. I tried the different focus reasons, no effect. I tried using QAction instead of plain QShortcut (even though I don't really need them), no success. I tried to set the focus on the main window (seemed wrong from the beginning...), no success. I tried to set the shortcut context to the different values (widget, widget with children, application), no change.

Am I missing something? Is this a PyQt bug or a known issue? Is something wrong with my approach? Can I have keyboard hits activate those buttons with a different approach?..

Many thanks for any help!

fispy
18th October 2016, 15:36
The problem was that the corresponding window was not the active window - a call to showFullscreen /activateWindow made it work.