While defining the custom signal emission from within QTableView
's keyPressEvent()
method:
def keyPressEvent(self, e):
if e.text()=='w':
self.emit(SIGNAL('writeRequested'))
if e.text()=='o':
self.emit(SIGNAL('openRequested'))
I am using incoming e
argument to figure out what keyboard key was pressed. With this "technique" I am limited to only one character at the time. Secondly, I am not able to use a combination of Ctrl+Key
, Alt+Key
or Shift+Key
. Third, I can't figure out what Delete
and Backspaces
keys are so I could compare them against e.text()
.
So there are few questions really...
Where in Qt docs all the keyboardKeys are listed so they could be used to do a
e.text()=='keyboardKey'
comparison.How to handle the double-keyboard keys combinations (such as
Ctrl+Key
) with the "technique" I am using (sending a custom signal from inside of view'skeyPressEvent()
?Is there an alternative easier way to hook the keyboard keys to trigger a method or a function (so the user could use keyboard shortcuts while mouse positioned above
QTableView
to launch the "action")?
If you look at the signature of
keyPressEvent()
you will see that thee
argument you describe in your question is of typeQKeyEvent
.QKeyEvent
instances have a methodkey()
which returns an integer that can be matched against constants in the enumQt.Key
.For example:
Similarly,
QKeyEvent
has a methodmodifiers()
. Because there can be multiple keyboard modifiers pressed at once, you need to use this a little differently. The result frommodifiers()
is the binary OR of one or more of the constants in theQt.KeyboardModifier
enum. To test if a given modifier is pressed, you need to perform the binary AND. For example:Note: In the example above, if both ctrl+shift were pressed, then all three if statements execute, in sequence.
Just for completeness and if you want more difficult sequences (ctrl-c followed by ctrl-k for example) just use QKeySequence as shortcut of a QAction which can be added to any QWidget.