在 Dialog 和 MessageDialog 上,我们如何更改 qml 中按钮的文本?

Nma*_*r88 6 qt qml

假设我们使用 aDialog或 aMessageDialog元素,其中有默认按钮。我们如何更改文本甚至使用 qsTr 进行翻译?

让我们假设以下情况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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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")
}
Run Code Online (Sandbox Code Playgroud)

正如我们所看到的,它使用的StandardButton是我想要的可自定义文本。

小智 7

我解决了调用该方法的问题

myDialog.standardButton(Dialog.Ok).text = qsTrId("Ok")
Run Code Online (Sandbox Code Playgroud)

Component.onCompleted