Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. dialog when closing window
Forum Updated to NodeBB v4.3 + New Features

dialog when closing window

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 3 Posters 2.5k Views 1 Watching
  • 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 Offline
    S Offline
    summerfang
    wrote on last edited by summerfang
    #1

    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
    0
    • R Offline
      R Offline
      Roumed
      wrote on last edited by
      #2

      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
      0
      • R Roumed

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

        S Offline
        S Offline
        summerfang
        wrote on last edited by
        #3

        @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
        0
        • V Offline
          V Offline
          Vicky Sharma
          wrote on last edited by Vicky Sharma
          #4

          You want quit only application window?

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

          1 Reply Last reply
          0
          • S summerfang

            @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 Offline
            R Offline
            Roumed
            wrote on last edited by
            #5

            @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
            0
            • R Roumed

              @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 Offline
              S Offline
              summerfang
              wrote on last edited by
              #6

              @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
              0
              • R Offline
                R Offline
                Roumed
                wrote on last edited by
                #7

                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
                0
                • R Roumed

                  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 Offline
                  S Offline
                  summerfang
                  wrote on last edited by
                  #8

                  @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
                  0
                  • S summerfang

                    @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 Offline
                    R Offline
                    Roumed
                    wrote on last edited by
                    #9

                    @summerfang My bad.
                    Dialog blocks closing.

                    onYes: {
                        root.isCloseAdmitted = true;
                        close()
                        root.close();
                    }
                    
                    S 1 Reply Last reply
                    0
                    • R Roumed

                      @summerfang My bad.
                      Dialog blocks closing.

                      onYes: {
                          root.isCloseAdmitted = true;
                          close()
                          root.close();
                      }
                      
                      S Offline
                      S Offline
                      summerfang
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved