1

I use QML to build GUI in my app + simple logics. At some step I open dialog and after closing it I want to get back a result value. This is sample code:

Button {
  id: myButton
  onClicked: {
    var component = Qt.createComponent("Dialog.qml");
    if (component.status === Component.Ready) {
        var dialog = component.createObject(parent);
        dialog.show();
        dialog.onClosing: {} // that not works
    }
  }
}

Dialog.qml:

import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.1

Window {
  id: dialogWindow
  width: 800
  height: 600
  flags: Qt.Dialog

  Button {
    id: closeButton
    onClicked: {
      dialogWindow.close();
    }
  }
}

But I have no idea how can I get some return value after the dialog was closed.

folibis
  • 12,048
  • 6
  • 54
  • 97

1 Answers1

3

My advice would be to not bother with Window and use Qt Quick Dialogs.

If you want to use Window, you have to define your own return value in the form of some Yes/No buttons in the window, for example. When one of these is clicked, set a yes property to true/false. Then, within your Button:

Connections {
    target: dialogWindow
    onVisibleChanged: // some action depending on value of "yes" property
}

Note that I used onVisibleChanged instead of onClosing, as the latter is only emitted upon the user closing the window:

This signal is emitted when the user tries to close the window.

If we follow the documentation for the CloseEvent argument, we see that it's explained in slightly more detail:

Notification that a window is about to be closed by the windowing system (e.g. the user clicked the titlebar close button).

Mitch
  • 23,716
  • 9
  • 83
  • 122
  • Thanks @Mitch, the idea with Connections is exactly what I need. – folibis Jul 06 '14 at 22:04
  • So, I use `onClosing` signal. Unfortunately, it fires when I close the window with x button and not works when I close the window from the code: `dlg.close()`. Maybe some bug in QML? – folibis Jul 06 '14 at 22:09
  • Yeah, that's why I suggested `onVisibleChanged`. It's not a bug, it just could be documented a bit better. I've updated my answer. – Mitch Jul 07 '14 at 08:56