开发者

Best approach to retrieve values from a QML Modal dialog

开发者 https://www.devze.com 2023-03-02 21:39 出处:网络
In my QT C++ application i call a QML ModalDialog with 2 buttons (OK/CANCEL), which displays correctly on screen and so, no problem there.

In my QT C++ application i call a QML ModalDialog with 2 buttons (OK/CANCEL), which displays correctly on screen and so, no problem there.

However i'm struggling to find a way to retrieve in my QT C++ application which button was pressed.

I'm unable to somehow "freeze" when i call the QML ModalDialog, to wait there until the user press OK Button or Cancel Button What i see is that application calls the QML ModalDialog, and immediately exit that part and continue.

QM开发者_JS百科etaObject::invokeMethod can call a QML function and have a return value, but it just doesn't wait for the user press one of the buttons, it just exits immediately, so no use.

I want to use this QML ModalDialog in several places of my application (the QML modal dialog can have different text passed from my QT C++ application), so i was looking to a generic solution for this.

Basically and generic speaking i'm looking for something like this:

C/C++ return_value = QML_Modal_Dialog(....)

Can someone point me in the right direction? Thanks


QML modal dialog comes with two signals 'accepted' and 'cancelled'. If you provide handlers for these two signals in your code, you would be able to know which button got pressed. You can refer to the below for reference.

Modal Dialog Ref 1

Modal Dialog Ref 2

Hope this helps!


Despite that the question is too old maybe my answer will help someone else.

I faced the same problem with Dialogs in QML. You think about it in imperative way, while QML is a declarative language that doesn't allow you to stop the flow of program and wait for the user's choice. As Purnima suggested you should use signal handlers (you can find the list of them for Qt 5.6 here). Move some of your app's logic to the signal handlers.

For example - if your function a() is executed based on the user's choice in dialog you should instead call it inside the dialog in its signal handlers (e.g. onAccepted or onRejected) with two different parameters based on the type of signal handler. Think about it as splitting the flow in two streams.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号