C ++ Qt QShortcut with numpad key

QShortcut makes it easy to connect a QShortcutEvent (keystroke, combination, or sequence) to a slot method, for example:

QShortcut *shortcut = new QShortcut( QKeySequence(Qt::Key_7), this, 0, 0, Qt::ApplicationShortcut); 

(Hint: for numeric keys, QSignalMapper can be used to map the activated() signal of QShortcut activated() to the slot with the int parameter).

However, in this example, with NumLock ( numpad enabled), both keys "7" will trigger the activated() signal of the activated() shortcut.

Is there a way to detect other keys besides filtering or overriding the keyPressEvent widget and checking QKeyEvent :: modifiers () for Qt :: KeypadModifier ?

Digging further, I found

QTBUG-20191 Qt :: KeypadModifier does not work with setShortcut link to a patch that was merged into 4.8 in September 2012 and that comes with a test case using

 button2->setShortcut(Qt::Key_5 + Qt::KeypadModifier); 

which does not work for my QShortcut on Qt 4.8.1, that is, none of the '7' keys is recognized by (adding) a modifier flag.

So I think the fastest way would be to set a filter to detect the modifier and let all other keyEvents be handled by the default implementation for use with QShortcut?

+5
source share
2 answers

For this you can use keyReleaseEvent (QKeyEvent * event) For example

 void Form::keyReleaseEvent(QKeyEvent *event) { int key = event->nativeScanCode(); if( key == 79 ) //value for numpad 7 { //your statement } } 
+2
source

You can use Qt.KeypadModifier , for example [Python]:

 def keyPressEvent(self, event): numpad_mod = int(event.modifiers()) & QtCore.Qt.KeypadModifier if event.key() == QtCore.Qt.Key5 and numpad_mod: #Numpad 5 clicked 
+1
source

Source: https://habr.com/ru/post/1492035/


All Articles