Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to detect Dialog.Discard



  • 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?'
        }
    }


  • bump .


  • Qt Champions 2017

    what is bump here ?

    Have you tried onDiscard signal ?



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


  • Qt Champions 2018

    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();
                }
            }
        }


  • @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 :)


Log in to reply