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

时间:2019-02-27 08:39:52

标签: qt qml

让我们假设我们使用的是DialogMessageDialog元素,其中有默认按钮。我们如何更改文本甚至使用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)
    }
}

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")
}

我们可以看到它使用StandardButton,这是我想要自定义文字的内容。

1 个答案:

答案 0 :(得分:0)

我解决了调用方法的问题

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

Component.onCompleted