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. How to detect Dialog.Discard
Forum Update on Monday, May 27th 2025

How to detect Dialog.Discard

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 766 Views
  • 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.
  • T Offline
    T Offline
    Tom_H
    wrote on last edited by
    #1

    Hello. I have a QuickControls2 Dialog where I have defined standardButtons: Dialog.Save | Dialog.Discard . How do I detect when the Discard button is clicked? It does not close the dialog or emit a rejected signal. It does nothing. The documentation says it has the mysterious DestructiveRole, but doesn't explain how to use it. Thanks.

    Dialog {
        id: savdlg
        standardButtons: Dialog.Save | Dialog.Discard
        // ...
    
        Component.onCompleted: {
            console.log('savdlg completed')
        }
    
        Component.onDestruction: {
            console.log('savdlg destruction')
        }
    
        onAccepted: {
            console.log('savdlg: accepted')
        }
    
        onRejected: {
            console.log('savdlg: rejected')
        }
    
        Text {
            text: 'Save changes?'
        }
    }
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tom_H
      wrote on last edited by
      #2

      bump .

      1 Reply Last reply
      0
      • dheerendraD Online
        dheerendraD Online
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        what is bump here ?

        Have you tried onDiscard signal ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        T 1 Reply Last reply
        0
        • dheerendraD dheerendra

          what is bump here ?

          Have you tried onDiscard signal ?

          T Offline
          T Offline
          Tom_H
          wrote on last edited by Tom_H
          #4

          @dheerendra Bump gets the topic back to the top.

          Ah, I should have specified I was using Qt 5.9. There is a Discard button and a DestructiveRole but no discarded signal. I see they added it in 5.10. Thanks.

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #5

            You could use the fact that the footer of a Dialog is a DialogButtonBox by default and add this to your dialog:

                signal discarded()
                Connections {
                    target: dialog.footer
                    onClicked: {
                        if (button.DialogButtonBox.buttonRole) {
                            dialog.discarded();
                            dialog.close();
                        }
                    }
                }
            
            T 1 Reply Last reply
            2
            • GrecKoG GrecKo

              You could use the fact that the footer of a Dialog is a DialogButtonBox by default and add this to your dialog:

                  signal discarded()
                  Connections {
                      target: dialog.footer
                      onClicked: {
                          if (button.DialogButtonBox.buttonRole) {
                              dialog.discarded();
                              dialog.close();
                          }
                      }
                  }
              
              T Offline
              T Offline
              Tom_H
              wrote on last edited by
              #6

              @GrecKo Thank you! That worked. I could have also used a Cancel button and changed the text to 'Discard', but I did something else instead. I now want both a Cancel and Discard button, but don't like the default role for Cancel, so I made my own custom DialogButtonBox for the footer.

              Glad I bumped :)

              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