[PyQt] Keyboard shortcuts not working

Ulrich Berning ulrich.berning at denviso.de
Fri Apr 11 10:49:34 BST 2008

Karlo Lozovina wrote:

>On 4/11/08, Ulrich Berning <ulrich.berning at denviso.de> wrote:
>> From the docs:
>> "Actions are added to widgets using QWidget::addAction(). Note that an
>>action must be added to a widget before it can be used; this is also true
>>when the shortcut should be global (i.e., Qt::ApplicationShortcut as
>> Add the line
>>  self.addAction(self.action)
>> and it works.
>Thanks (both Ulrich and Mark), that's it!
>I re-read that part of the docs at least twice, and still didn't
>notice that :(. And the book I'm using adds the action to a
>toolbar/menubar, no mention of what to do when the application has
>Btw, is there any difference using QAction over QShortcut, for
>applications that don't have toolbar/menubar?
If you don't have a toolbar/menubar, it's better to use QShortcut. It is 
less code and you don't have to provide an icon and a button text, that 
are never used.

Try it yourself, both code snippets should give the same result:

shortcut = QShortcut(self)
self.connect(shortcut, SIGNAL("activated()"), self.down)

action = QAction(QIcon(), "Down", self)
self.connect(action, SIGNAL("triggered()"), self.down)


More information about the PyQt mailing list