Search code examples
buttonqmlmessagedialog

How to set the default button of a MessageDialog in QML?


The default button is "Yes", but I want to set the button "No" as the default button.

How to do it?


Solution

  • I don't see any way to achieve this through the current MessageDialog API, but I'd also imagine that this is very much platform-specific, and that's why it's hidden.

    You can make your own dialog, though:

    import QtQuick 2.3
    import QtQuick.Window 2.0
    import QtQuick.Controls 1.2
    import QtQuick.Dialogs 1.2
    
    Window {
        width: 500
        height: 500
        visible: true
    
        Dialog {
            id: dialog
            visible: true
    
            contentItem: FocusScope {
                Row {
                    anchors.bottom: parent.bottom
                    anchors.right: parent.right
    
                    Button {
                        text: "No"
                        isDefault: true
                        focus: true
                        onClicked: dialog.close()
                    }
                    Button {
                        text: "Yes"
                    }
                }
            }
        }
    }
    

    dialog