Nmaster88
Nmaster88

Reputation: 1585

On a Dialog and MessageDialog how can we change the text of buttons in qml?

Lets suppose we are using a Dialog or a MessageDialog element, where we have the default buttons. How can we change the text and even use qsTr for translating?

Lets suppose the following Dialog

Dialog {
    id: dateDialog
    visible: true
    title: "Choose a date"
    standardButtons: StandardButton.Save | StandardButton.Cancel

    onAccepted: console.log("Saving the date " +
        calendar.selectedDate.toLocaleDateString())

    Calendar {
        id: calendar
        onDoubleClicked: dateDialog.click(StandardButton.Save)
    }
}

and MessageDialog:

import QtQuick 2.2
import QtQuick.Dialogs 1.1

MessageDialog {
    title: "Overwrite?"
    icon: StandardIcon.Question
    text: "file.txt already exists.  Replace?"
    detailedText: "To replace a file means that its existing contents will be lost. " +
        "The file that you are copying now will be copied over it instead."
    standardButtons: StandardButton.Yes | StandardButton.YesToAll |
        StandardButton.No | StandardButton.NoToAll | StandardButton.Abort
    Component.onCompleted: visible = true
    onYes: console.log("copied")
    onNo: console.log("didn't copy")
    onRejected: console.log("aborted")
}

As we can see it uses StandardButton this is the one i want the text customizable.

Upvotes: 6

Views: 2764

Answers (1)

Capraro Ivan
Capraro Ivan

Reputation: 71

I solved the problem calling the method

myDialog.standardButton(Dialog.Ok).text = qsTrId("Ok")

in Component.onCompleted

Upvotes: 7

Related Questions