QKeySequence for QKeyEvent

I am in a situation when I try to read in the JSON configuration file, which determines which key commands correspond to the given actions. For example:

... { "Action": "Quit", "Combo" : "CTRL+Q" }, ... 

Building a QKeySequence from a combined tag is trivial, but I need to track the QKeyEvent to trigger the actions. Please note that I have to track QKeyEvent , because they are used for other purposes in the application. that is, it would be unacceptable to control only key commands for QKeySequence (if possible).

With the exception of writing a custom analyzer to create a QKeyEvent object for each "Combo" tag, is there any comparison between QKeyEvent and QKeySequence ? For example:

 QKeyEvent KeyCommandsHandler::toKeyEvent(QKeySequence sequence) { //somehow convert to QKeyEvent } 
+8
c ++ qt key-events qkeysequence
source share
3 answers

Simple solution (written in python):

 key = QKeySequence(event.modifiers()|event.key()).toString() 

Show you the whole sequence in string form, for example "Ctrl + Q".

The benefits (at least in python) that you can find as shortcuts, while QKeySequence would not be hashed.

Beware that this assumes that you are using the correct typecase and spacing. "ctrl + Q" will not match. To avoid all the problems, you can do the following when you read the shortcuts for the first time:

 shortcut = shortcut.lower().remove(' ') 

and match / find with

 key = QKeySequence(event.modifiers()|event.key()).toString().lower() 

or better yet:

 shortcut = QKeySequence(shortcut).toString() 

and direct match.

+4
source share

In general, you cannot compare QKeyEvent and QKeySequence . QKeyEvent represents a single key press or release event, while QKeySequence can contain a sequence of four keys, each with additional modifier information.

However, you can compare objects if you are sure that there will always be only one key in your key sequences:

 bool isEquiv(const QKeyEvent& event, const QKeySequence& seq) { if (seq.count() != 1) return false; return seq[0] == (event.key() | event.modifiers()); } 

You can even write a conversion function for QKeyEvent to QKeySequence :

 QKeySequence toKeySequence(const QKeyEvent& event) { return QKeySequence(event.key() | event.modifiers()); } 

Note that it makes no sense to convert QKeySequence to QKeyEvent , although you need to select a specific type of event, for example QEvent::KeyPress or QEvent::KeyRelease .

+3
source share

Qt4.7 note with code for converting KeyEvent to KeySequence. (But the code has drawbacks because it distinguishes int for the key code from QKeyEvent.key () in a string. Is it better to use QKeyEvent.text ()?)

In addition, the code in Ferdinand answers:

 QKeySequence(event.key() | event.modifiers()) 

is not type safe (mixes int and QKeyboardModifiers), and if the converted to Python fails in PyQt, but not in PySide?

Also, “QKeyEvent represents a single key press or release event” does not actually explain this. QKeyEvent can tell you which key combination was omitted, not the order in which they were pressed. When a user presses keys sequentially, your application may receive a sequence of QKeyEvents (depending on whether your application uses the default or overridden handlers for QKeyEvent.) Later QKeyEvents will show you all the keys that were at the time of the event. They can no longer sleep . This is pretty tricky.

0
source share

All Articles