Wait for dialog response

  • Hi all,

    I'm porting QtWidgets app to QML, so C++ logic remains.
    In spite of QML declarativeness, is it possible somehow to call QML dialog from C++ and wait until the dialog window will return an answer? Similar to QDialog::exec()

  • Lifetime Qt Champion


    What kind of dialog do you want to call ?

  • Any...
    let's say Dialog of QML.

    To be precise, I have many places when C++ routines called QMessageBox or custom QDialog.
    So it is head-breaking how to split every of those functions and do not spoil the logic that just works.
    So if there is any way how to ie.:

    • from a place where dialog was called, send a signal for a dialog
    • then freeze C++ there by some mutex
    • respond for a signal and perform a dialog on QML side
    • get returned value and unlock the mutex
    • continue C++ routine with returned value

    But I'm not sure is it possible or wise and maybe there is easiest way to do it,
    so any clue is appreciated

  • Lifetime Qt Champion

    Can you show an example of such a code path ?

  • void someMethod(int param) {
      int var1, var2;
      // some preparation for dialog...
      // C++ way to display a dialog and wait for response
      auto myDialog = new MySummaryDialog(param);
      int returnValue = mySummaryDialog::exec();
      // possible QML way: emit getSummaryDialog(param);
      // HOW TO lock code here until QML dialog close
      // continuation of someMethod() routines, depends on many variables declared inside
      // a few combinations of:
      if (returnValue == var1) {
        // ...
      } else if (returnValue == var2) {
        // ...
      } // else etc...

  • @SeeLook what about using Dialog component from QML? It implements pretty much the same paradigm as old QDialog... See example

  • @Pablo-J.-Rogina
    Thanks for response.

    Unfortunately all QML dialogues (either Dialog or MessageBox) exists in parallel with code that invoked it.

    Dialog {
      id: someDialog
    function callDialog() {
      // I would expect here to know what user did to the dialog but it is not a case of QML (declarative programming)

    So I'm asking is it possible to bend this situation?

  • Qt Champions 2017

    QML dialogs can be modal too ( modal = wait for ok/cancel)

    Dialog {
        id: dialog
        modal: true
        standardButtons: Dialog.Ok

  • @SeeLook I was able to modify DialogPage.qml from this example I mentioned and by adding a Label at the top level (just after existing Label) I can display what the user input after closing "Input" dialog that pops up when clicking "Input" button in the example...

                text: "Dialog is a popup that is mostly used for short-term tasks "
                    + "and brief communications with the user."
            Label {
                id: result
                width: parent.width
                wrapMode: Label.Wrap
                horizontalAlignment: Qt.AlignHCenter
            Button {
                text: "Message"
                Dialog {
                    id: inputDialog
                    standardButtons: Dialog.Ok | Dialog.Cancel
                    onAccepted: {
                        result.text = "user: " + user.text + " password: " + password.text
                        TextField {
                            id: user
                            focus: true
                            placeholderText: "Username"
                            Layout.fillWidth: true
                        TextField {
                            id: password
                            placeholderText: "Password"
                            echoMode: TextField.PasswordEchoOnEdit
                            Layout.fillWidth: true

  • @mrjj , @Pablo-J-Rogina
    Thank You for the clue.
    But Dialog of Qt Quick 2 (which has modal property) is rather like a popup - it has no any titlebar and frame. I'm using that one from import QtQuick.Dialogs 1.3.
    However I will reconsider it.
    This dialog business is messy for me....

  • @SeeLook I'm not trying to convince you about Quick Controls 2, but I think it's the way to go.
    Also, to make an informed decision please take a look at Dialog details:

    Dialog's title is displayed by a style-specific title bar that is assigned as a dialog header by default.

  • To Quick Controls 2 I'm rather convinced, with this single exception... :-)
    But really, You opened my eyes for some facts about those dialogs. I have to digest that and weigh what will be more suitable for my app.
    So I'm quite satisfied with Your answers.
    Thank You a lot.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.