Results 1 to 2 of 2

Thread: QShortcut not working due to focus problem - even on top window

  1. #1
    Join Date
    Oct 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QShortcut not working due to focus problem - even on top window

    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.

    Qt Code:
    1. button = QPushButton (...)
    2. key = self.nr_items () # some integer
    3. button.setText ('[' + str (key) + ']' + button.text ())
    4. toplevelWidget = ... # the MainWindow instance
    5. ...
    6. shortcut = QShortcut (QKeySequence (key + Qt.Key_0), toplevelWidget)
    7. shortcut.activated.connect (button.click)
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Oct 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QShortcut not working due to focus problem - even on top window

    The problem was that the corresponding window was not the active window - a call to showFullscreen /activateWindow made it work.

Similar Threads

  1. Python / PyQt5 MDI Window focus problem
    By apereira in forum Newbie
    Replies: 5
    Last Post: 21st September 2015, 23:29
  2. QShortcut not always working
    By jörg in forum Qt Programming
    Replies: 7
    Last Post: 8th June 2011, 16:59
  3. Window Focus Problem
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2010, 13:34
  4. Window focus issues (How to force focus to a window?)
    By montylee in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2009, 01:00
  5. QShortcut not working
    By berinder in forum Qt Programming
    Replies: 0
    Last Post: 24th November 2008, 10:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.