diff --git a/Action.qml b/Action.qml index c13fa23..1de535a 100644 --- a/Action.qml +++ b/Action.qml @@ -62,6 +62,14 @@ Item { onToggled: root.toggled() } + Component.onCompleted: { + // Forwarding to MenuBar. Little hack to make Action shortcuts work properly, because otherwise, they would not trigger + if(shortcut != 0) { + console.log("Adding shortcut for", text, "with shortcut", shortcut) + parent.parent.addShortcut(shortcut, root.triggered) + } + } + /*! \qmlmethod void Action::toggle() Toggles the \c checked state to its opposite state. @@ -70,4 +78,10 @@ Item { root.trueItem.toggle() } + /*! + \qmlmethod void Action::addShortcut(keysequence sequence, var trigger) + + */ + function addShortcut(sequence, trigger) { + } } diff --git a/Menu.qml b/Menu.qml index eaedca9..04ba777 100644 --- a/Menu.qml +++ b/Menu.qml @@ -192,4 +192,13 @@ Item { controlsMenu.popup() } } + + /*! + \qmlmethod void Menu::addShortcut(keysequence sequence, var trigger) + Forwarding to MenuBar. Little hack to make Action shortcuts work properly, because otherwise, they would not trigger + \sa MenuBar::addShortcut + */ + function addShortcut(sequence, trigger) { + parent.parent.addShortcut(sequence, trigger) + } } diff --git a/MenuBar.qml b/MenuBar.qml index 690326f..18c091a 100644 --- a/MenuBar.qml +++ b/MenuBar.qml @@ -82,4 +82,19 @@ Item { function clear(){ root.trueItem.clear() } + + /*! + \qmlmethod void MenuBar::addShortcut(keysequence sequence, var trigger) + Little hack to make Action shortcuts work properly. + */ + function addShortcut(sequence, trigger) { + var newShortcut = Qt.createQmlObject(` + import QtQuick 2.12; + Shortcut { + context: Qt.ApplicationShortcut + }`, root, "shortcut" + sequence); + newShortcut.sequence = sequence + newShortcut.activated.connect(trigger) + console.log("Added shortcut " + sequence) + } }