Reputation: 3075
I have a Qt Quick Controls 2 Action with a shortcut:
Action {
id: myAction
shortcut: "Ctrl+D"
}
How can I get a platform native representation of the shortcut?
Here is what I already tried:
Using the shortcut right away like
ToolTip.text: myAction.shortcut
However, this returns the shortcut as defined (for example, "Ctrl+D"
) and not a platform native representation (for example, "⌘D"
). It also returns incorrect results in case a StandardKey
is used because it gives the integer value of the StandardKey
and not the corresponding key combination.
Using a nativeText
property like
ToolTip.text: myAction.shortcut.nativeText
But such a property doesn't exist.
Upvotes: 3
Views: 814
Reputation: 3075
There seems to be no straight forward way to get this.
A workaround is:
Shortcut
itemnativeText
property.Action
and add a custom property to hold the shortcutshortcut
property of the Action
will always return a string which will be interpreted incorrectly by the Shortcut
item when using a StandardKey
(the Shortcut
item will interpret the integer value as the shortcut so you get a "3"
instead of "Ctrl+O"
).Action
shortcut and to the Shortcut
sequence So in code:
CustomAction.qml
import QtQuick 2.7
import QtQuick.Controls 2.4
Action {
/* Custom property */
property var keySequence
shortcut: keySequence
}
ToolTipButton.qml
import QtQuick 2.7
import QtQuick.Controls 2.4
Button {
/* Disabled Shortcut */
Shortcut {
id: dummyShortcut
enabled: false
sequence: action.keySequence
}
hoverEnabled: true
ToolTip.visible: hovered
ToolTip.delay: 1000
ToolTip.timeout: 5000
ToolTip.text: dummyShortcut.nativeText
}
Used like this:
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.4
ApplicationWindow {
visible: true
CustomAction {
id: myAction
keySequence: StandardKey.Open
}
ToolTipButton {
id: myButton
action: myAction
text: "Trigger my action"
}
}
Upvotes: 2