I'm in a situation whereby I am trying to read in a JSON config file which dictates what key commands map to given actions. For example:
...
{
"Action": "Quit",
"Combo" : "CTRL+Q"
},
...
Constructing a QKeySequence from the combo tag is trivial but I need to monitor QKeyEvents in order to trigger actions. Please note I have to monitor QKeyEvents because they are used for other purposes in the application as well. i.e. it would not be acceptable to only monitor key commands for QKeySequences (if that is even possible).
Short of writing a custom parser to construct a QKeyEvent object for each "Combo" tag, is there anyway of comparing a QkeyEvent to a QKeySequence? For example:
QKeyEvent KeyCommandsHandler::toKeyEvent(QKeySequence sequence) {
//somehow convert to QKeyEvent
}
In general, you cannot compare
QKeyEventandQKeySequenceobjects.QKeyEventrepresents the event of a single key press or release, whereas aQKeySequencecan contain a sequence of up to four keys, each with optional modifier information.You can, however, compare the objects if you are sure that your key sequences will always contain just one key:
You can even write a conversion function for
QKeyEventtoQKeySequence:Note that it does not make sense to convert a
QKeySequenceto aQKeyEvent, though, since you have to choose a specific event type such asQEvent::KeyPressorQEvent::KeyRelease.