How to implement a modal dialog in QML and get it's return value?
All,
In Qt C++ lib, we can use dialog's exec() function and check it's return code to know whether the dialgo is accepted or rejected. In this way, I can get user's information easily by these code:
ret = dialog.exec();
if(ret == QDialog::Accepted)
{
// update information and do something
}
But I can not find exec() in QML. I can find open() function of dialog in QML, but it do not wait until dialog closed, and I can not find when to update information from dialog. Can anyone tell me how to use the modal function in QML?
Re: How to implement a modal dialog in QML and get it's return value?
Bellow is my solution. Is there any better solution?:(
Code:
Button {
id: buttonAppendDeviceList
text: qsTr("æ·»åŠ è®¾å¤‡")
property var addControler:AddControler{
modality: Qt.WindowModal
onAccepted: {
listControlerModel.append({"deviceID":modbusAddress,
"type":controlerType,
"firmwareType":contorlerTypeIndex,
"serialNumber":"",
"programEnabled":true,
"colorCode":"black",
"currentProgress":0});
}
}
onClicked: {
addControler.modality = Qt.WindowModal;
addControler.open();
console.log("addControler.open();");
console.log("listControler.currentIndex: ",listControler.currentIndex);
listControler.currentIndex = listControler.currentIndex + 1;
}
}
Re: How to implement a modal dialog in QML and get it's return value?
QtQuick/QML is a declarative environment. You cannot think of it in terms of an imperative language such as C++. Getting a modal dialog in QtQuick is easy (make your "dialog" visible and put a MouseArea underneath it that will catch all the mouse events to prevent them reaching other objects) but this has nothing to do with "waiting until dialog is closed". The latter means blocking control flow which doesn't make sense in QML. If you think you need that then first it means you are wrong, and then it means you have too much imperative code. Try specifying the effect you want to achieve and we'll try to help you reach it in a more declarative way.
Re: How to implement a modal dialog in QML and get it's return value?
QtQuick Control's Dialog base can do modal dialogs.
Cheers,
_
Re: How to implement a modal dialog in QML and get it's return value?
wysota and anda_skoa,
thank you! I understand it now.