dialog when closing window
-
I want to use a dialog when closing the window to make sure that the user really wants to close the current window( I don't want to quit qt, I just want to close the current window. There are also some other windows running in this project. ). And my code is something like
ApplicationWindow { id:root visible: true title: qsTr("Main Window") Dialog { id: adialog title: "Warning" standardButtons: StandardButton.Yes | StandardButton.Cancel Label { text: "Are you sure to close?" } onYes:{ root.close(); } } onClosing: {close.accepted = false ; adialog.open();} }
However, when I click the "Yes" on the dialog, it cannot close the window. I also tried to add something like " root.onClosing.close.accepted = true;" to the onYes propery of the dialog, but also failed. Anyone has idea about how to close the window properly using the dialog? thanks!!
-
In onYes slot you should call
Qt.quit();
.
http://doc.qt.io/qt-5/qml-qtqml-qt.html#quit-method -
In onYes slot you should call
Qt.quit();
.
http://doc.qt.io/qt-5/qml-qtqml-qt.html#quit-method@Roumed I don't want to quit qt, I just want to close the current window. There are also some other windows running in this project. I'll add this in this question description, thank you for pointing out that.
-
You want quit only application window?
If yes then you may try this 'root.visible = false'
-
@Roumed I don't want to quit qt, I just want to close the current window. There are also some other windows running in this project. I'll add this in this question description, thank you for pointing out that.
@summerfang Ow.
If so, I think, there is no one-string-way to solve this problem.
You can use some kind of flag to admit closing from dialog. -
@summerfang Ow.
If so, I think, there is no one-string-way to solve this problem.
You can use some kind of flag to admit closing from dialog.@Roumed Thank you, could you please give me a little more tips about how to set the flag? I tried different methods I could do but no one works...
-
Something like this:
ApplicationWindow { id:root visible: true title: qsTr("Main Window") Dialog { id: adialog title: "Warning" standardButtons: StandardButton.Yes | StandardButton.Cancel Label { text: "Are you sure to close?" } onYes:{ root.isCloseAdmitted = true; root.close(); } } property bool isCloseAdmitted: false onClosing: { if (!isCloseAdmitted) { close.accepted = false ; adialog.open(); } } }
-
Something like this:
ApplicationWindow { id:root visible: true title: qsTr("Main Window") Dialog { id: adialog title: "Warning" standardButtons: StandardButton.Yes | StandardButton.Cancel Label { text: "Are you sure to close?" } onYes:{ root.isCloseAdmitted = true; root.close(); } } property bool isCloseAdmitted: false onClosing: { if (!isCloseAdmitted) { close.accepted = false ; adialog.open(); } } }
@Roumed Thanks a lot, I tried this but the window does not close after I clicked "Yes". I must click the "x" once again after the dialog disappears.
-
@Roumed Thanks a lot, I tried this but the window does not close after I clicked "Yes". I must click the "x" once again after the dialog disappears.
@summerfang My bad.
Dialog blocks closing.onYes: { root.isCloseAdmitted = true; close() root.close(); }
-
@summerfang My bad.
Dialog blocks closing.onYes: { root.isCloseAdmitted = true; close() root.close(); }
@Roumed Oh, so it is! Thank you so much!!