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

Dialog standardButtons



  • Hello,

    I would like to know how to get the result of which button was press.
    For example I have yes, no , ok, cancel. If I click on no I know that it is rejected but I don't know if the user press no or cancel.

    Thank you,



  • Dialog {
        id: dialog
        standardButtons: Dialog.Yes | Dialog.No | Dialog.Ok | Dialog.Cancel
    
        Connections {
            target: dialog.footer
            onClicked: {
                if (button.text === dialog.standardButton(Dialog.Cancel).text)
                    console.log("Cancel clicked")
                else if (button.text === dialog.standardButton(Dialog.No).text)
                    console.log("No clicked")
            }
        }
    }
    

    It looks ugly, but it works



  • @Nifiro I am away from my computer at the moment but I believe it is possible to compare button directly with standardButton(Dialog.Cancel) etc.

    i.e.

    if (button === dialog.standardButton(Dialog.Cancel)) { 
       ...
    

    I seem to recall having to do this inside an explicit DialogButtonBox though. I don't see standardButton() defined as a method on Dialog, though I am stuck with using 5.9.6 at the moment and maybe it has changed since.

    Edit: Just looked at more recent doc and I still don't see standardButton() defined on Dialog itself, but maybe it is available but undocumented?


Log in to reply