How to use key sequences in qml ?
-
Hello, everyone! Now, I'd like to use Ctrl+Z to do the undo practice. However, how to do it in qml ? Does anyone know the answer ? Thanks in advance.
-
Rectangle { // or anything else Keys.onPressed: { if (event.key == Qt.Key_Undo) { // do your undo handling here event.accepted = true; } } }
If you ask about undo/redo handling in text edit fields - it is handled automatically. If you need some more advanced functionality, use the C++ part for that: QUndoCOmmand and friends. http://doc.qt.io/qt-5/qundo.html
-
As said on chat, if Key_Undo is not exported, you can either handle it in c++ or react to Key_Z and check if modifier is ctrl.
-
For a focused item, you can use KeyEvent::matches():
Item { focus: true Keys.onPressed: { if (event.matches(StandardKey.Undo)) myModel.undo() } }
If you want to handle it as a shortcut, regardless of which item has focus, use QML Shortcut:
Shortcut { sequence: StandardKey.Undo onActivated: myModel.undo() }
-
@jpnurmi said in How to use key sequences in qml ?:
For a focused item, you can use KeyEvent::matches()
[...]
If you want to handle it as a shortcut, regardless of which item has focus, use QML Shortcut [...]This is pure gold! Thanks!