In the following QML menu, I can get the shortcut for "File" and "New", but not for "Auto Save":
menuBar: MenuBar {
Menu {
title: qsTr("&File")
Action { text: qsTr("&New...") }
CheckBox {
id: autoSaveButton
checked: true
text: qsTr("&Auto Save")
action: autoSaveAction
}
}
Is the reason the fact that a CheckBox behaves differently from an Action?
I tried to move the CheckBox out of the menu, on the toolbar, with no improvement.
Qt version: 6.4
No need for a CheckBox
you can use an Action
or a MenuItem
and set the checkable
property to true
. A bit strange is that I needed to toggle the checked
property manually in the onToggled
or onTriggered
handler, otherwise the checked
of both MenuItem
and Action
wouldn't change automatically when using the shortcut even though toggled and triggered are called. But it does when clicking on the menu item.
menuBar: MenuBar {
Menu {
title: qsTr("&File")
Action { text: qsTr("&New...") }
Action {
id: autoSaveAction
checkable: true
checked: true
text: qsTr("&Auto Save")
onToggled: autoSaveAction.checked = !autoSaveAction.checked
}
}
}
This is probably due to the fact that you would set a setting via the toggle signal and bind that setting to the checked
property of the MenuItem
/Action
as it could be changed from somewhere else instead of binding to the checked
state of a MenuItem
/Action
. Something like that:
ApplicationWindow {
id: window
width: 320
height: 260
visible: true
property bool autoSave: true
menuBar: MenuBar {
Menu {
title: qsTr("&File")
Action { text: qsTr("&New...") }
Action {
id: autoSaveAction
checkable: true
checked: window.autoSave
text: qsTr("&Auto Save")
onToggled: window.autoSave = !window.autoSave
}
}
}
}