Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved dialog when closing window

    QML and Qt Quick
    3
    10
    1947
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      summerfang last edited by summerfang

      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!!

      1 Reply Last reply Reply Quote 0
      • R
        Roumed last edited by

        In onYes slot you should call Qt.quit();.
        http://doc.qt.io/qt-5/qml-qtqml-qt.html#quit-method

        S 1 Reply Last reply Reply Quote 0
        • S
          summerfang @Roumed last edited by

          @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.

          R 1 Reply Last reply Reply Quote 0
          • V
            Vicky Sharma last edited by Vicky Sharma

            You want quit only application window?

            If yes then you may try this 'root.visible = false'

            1 Reply Last reply Reply Quote 0
            • R
              Roumed @summerfang last edited by

              @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.

              S 1 Reply Last reply Reply Quote 0
              • S
                summerfang @Roumed last edited by

                @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...

                1 Reply Last reply Reply Quote 0
                • R
                  Roumed last edited by

                  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();
                          }
                      }
                  }
                  
                  S 1 Reply Last reply Reply Quote 0
                  • S
                    summerfang @Roumed last edited by

                    @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.

                    R 1 Reply Last reply Reply Quote 0
                    • R
                      Roumed @summerfang last edited by

                      @summerfang My bad.
                      Dialog blocks closing.

                      onYes: {
                          root.isCloseAdmitted = true;
                          close()
                          root.close();
                      }
                      
                      S 1 Reply Last reply Reply Quote 0
                      • S
                        summerfang @Roumed last edited by

                        @Roumed Oh, so it is! Thank you so much!!

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post