PDA

View Full Version : Making shortcut



kib2
28th June 2008, 13:47
Hi,

Im using PyQt, but it's a genral Qt4 question.

In an old app (PyQt4.3), I used this to make a shortcut on the "Tab" key with:

QtGui.QShortcut(QtGui.QKeySequence("Tab"), self.texteditor, self.doSomething)

Now, it doesn't work anymore in v4.4 (are there any changes about that behaviour ?), ie QtGui.QKeySequence("Tab") does not respond and I'm forced to use another key at the same time, like QtGui.QKeySequence("Ctrl+Tab").

So my question is simple : how do you set a shortcut on "Tab" key only ?

Thanks.

fullmetalcoder
28th June 2008, 14:37
The problem is probably that the tab key is associated to another action but that this is hardcoded (i.e done in a keyPressEvent and not through a QAction). The only way to work around this is to subclass the widget and reimplement the event handler according to your needs.

jpn
28th June 2008, 14:57
From QWidget docs (http://doc.trolltech.com/4.4/qwidget.html#events):

QWidget::keyPressEvent() is called whenever a key is pressed, and again when a key has been held down long enough for it to auto-repeat. Note that the Tab and Shift+Tab keys are only passed to the widget if they are not used by the focus-change mechanisms. To force those keys to be processed by your widget, you must reimplement QWidget::event().

kib2
28th June 2008, 16:52
Thanks for the tip, I'll try to do that way.

fullmetalcoder
28th June 2008, 18:20
Another method that might be worth reimplementing for simplicity : QWidget::focusNextPrevChild() (http://doc.trolltech.com/latest/qwidget.html#focusNextPrevChild)