I thought that this should have been a super common use case:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Universal 2.15
import QtQuick.Dialogs
Dialog {
id: rootItem
title: qsTr("Save unsaved changes?")
standardButtons: Dialog.Yes | Dialog.No | Dialog.Cancel
modal: true
Component.onCompleted: {
visible = false;
}
onAccepted: {
// Unsaved changes accepted
}
onRejected: {
// Unsaved changes rejected
}
}
How can I make a difference between user clicking on No
and Cancel
? The documentation seems to be very poor on this. Yes
means "accepted" and both No
and Cancel
means "rejected". In my application's state machine I want to know how the user rejected.
https://doc.qt.io/qt-6/qml-qtquick-controls-dialog.html
Dialog.No A "No" button defined with the NoRole.
What NoRole
? What should I do with this undocumented NoRole
?
I feel stupid.
We use a custom DialogButtonBox
as footer in our dialogs to handle this. By adding the buttons by hand you have full control about what is happening:
Dialog {
id: customDialog
title: qsTr("Save unsaved changes?")
footer: DialogButtonBox {
Button {
text: qsTr("Yes")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
onClicked: console.log("Yes clicked");
}
Button {
text: qsTr("No")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
onClicked: console.log("No clicked");
}
Button {
text: qsTr("Cancel")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
onClicked: console.log("Cancel clicked");
}
}
}