Results 1 to 6 of 6

Thread: Can't get keyboard shortcuts to work

  1. #1
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Can't get keyboard shortcuts to work

    Using PyQt4, I can't get keyboard shortcuts to work with the Qt.WidgetShortcut or Qt.WidgetWithChildrenShortcut contexts (nothing happens when I press the shortcut). I'm pretty sure its something simple I'm missing, but I just can't figure out what. Here is a sample class showing the steps I'm using. Can anyone point out what I'm missing?

    Qt Code:
    1. class Widget(QtGui.QWidget):
    2. def __init__(self):
    3. super().__init__()
    4. self.act = QtGui.QAction('test', self)
    5. self.act.setShortcut(QtGui.QKeySequence(QtGui.QKeySequence.Copy))
    6. self.act.setShortcutContext(Qt.WidgetWithChildrenShortcut)
    7. self.addAction(self.act)
    8. self.act.triggered.connect(self.slot)
    9.  
    10. def slot(self):
    11. print('triggered')
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Can't get keyboard shortcuts to work

    Can anyone help?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can't get keyboard shortcuts to work

    I don't know PyQt but mustn't have the widget the focus that the shortcut is working? And a QWidget normally don't get the focus.

  4. #4
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Can't get keyboard shortcuts to work

    Thanks for the reply, but that's not it. I tried it with other widgets and explicitly setting focus (and using hasFocus() to confirm).

  5. #5
    Join Date
    Jan 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Can't get keyboard shortcuts to work

    I don't know PyQt either, but you can try this (it's C++ but you should be able to read it) - and if it's working you can alter the code line by line (and post a reply, what the reason was):
    Qt Code:
    1. QAction* pAction = new QAction( "text4ESC", this );
    2. // Use a common key
    3. pAction->setShortcut( Qt::Key_Escape );
    4. // If using the Qt::WindowShortcut, you don't have to take care of the focus - just any element of the window must be focused
    5. pAction->setShortcutContext( Qt::WindowShortcut );
    6. connect( pAction, SIGNAL(triggered()),
    7. this, SLOT(SL_DoSomething()) );
    8. addAction( pAction );
    9.  
    10. setContextMenuPolicy( Qt::ActionsContextMenu );
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2012
    Posts
    7
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Can't get keyboard shortcuts to work

    Thanks for the example code, but I've just discovered the problem. In my simplified example, the focus was indeed the problem, but in my actual code (which I tested more extensively) I forgot to call widget.addAction()! The way I'm dealing with them is through several modules and a lot of abstraction, and somehow that just slipped through the gap.

    Thanks for the help though!

Similar Threads

  1. Keyboard shortcuts (Mac specific?)
    By lutz in forum Newbie
    Replies: 0
    Last Post: 25th February 2011, 00:57
  2. Qt Creator Set keyboard shortcuts for home and end key on mac
    By dpatel in forum Qt Tools
    Replies: 1
    Last Post: 7th February 2011, 21:32
  3. Enumerating all keyboard shortcuts
    By jonks in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2010, 21:59
  4. dynamic keyboard shortcuts troubles
    By gadnio in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2006, 10:48
  5. Keyboard shortcuts problem.
    By Lemming in forum Qt Programming
    Replies: 4
    Last Post: 5th April 2006, 16:12

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.