Hi,
with QMessageBox we have the possibility to use addButton() for adding customized button names and roles (or even QAbstractButtons) e.g. to add a "no harm" or let the user choose between options like "create link", "delete", "ignore".
Its QML sibling MessageDialog doesn't seem to have a similar feature. Is there any way to add differently named buttons without implementing such a dialog from scratch by myself?
I have found an ugly hack to do it by just altering the text property of the StandardButtons. This can be done by recursively iterating through the dialog's children, checking their text property for the StandardButtons' "normal" strings. Of course, this implementation is Locale-specific and might be subject to change in future versions of Qt...
Some excerpts from this hack:
QtObject {
id: priv
property string okStandard: "OK"
property string openStandard: "Öffnen"
property string saveStandard: "Speichern"
[...]
}
function replaceButtonTextParser(objectToParse) {
if (!replaceButtonTextParser.found) {
for (var i = 0; i < objectToParse.children.length; i++) {
if (objectToParse.children[i].text !== "") {
replaceButtonTextParser.found = true;
if (messageDialogItem.okText !== "" && objectToParse.children[i].text === priv.okStandard) {
objectToParse.children[i].text = messageDialogItem.okText
} else if (messageDialogItem.openText !== "" && objectToParse.children[i].text === priv.openStandard) {
objectToParse.children[i].text = messageDialogItem.openText
} else [if ... else] {
replaceButtonTextParser.found = false;
}
}
}
if (!replaceButtonTextParser.found) {
for (i = 0; i < objectToParse.children.length; i++) {
replaceButtonTextParser(objectToParse.children[i])
}
}
}
}
function replaceButtonText() {
replaceButtonTextParser.found = false;
if (messageDialogItem.enableButtonRenaming) {
replaceButtonTextParser(messageDialogItem.contentItem)
}
}
MessageDialog {
id: messageDialogItem
property bool enableButtonRenaming: false
property string okText: ""
property string openText: ""
property string saveText: ""
[...]
Component.onCompleted: {
replaceButtonText()
}
QtObject {
id: priv
property string okStandard: "OK"
property string openStandard: "Öffnen"
property string saveStandard: "Speichern"
[...]
}
function replaceButtonTextParser(objectToParse) {
if (!replaceButtonTextParser.found) {
for (var i = 0; i < objectToParse.children.length; i++) {
if (objectToParse.children[i].text !== "") {
replaceButtonTextParser.found = true;
if (messageDialogItem.okText !== "" && objectToParse.children[i].text === priv.okStandard) {
objectToParse.children[i].text = messageDialogItem.okText
} else if (messageDialogItem.openText !== "" && objectToParse.children[i].text === priv.openStandard) {
objectToParse.children[i].text = messageDialogItem.openText
} else [if ... else] {
replaceButtonTextParser.found = false;
}
}
}
if (!replaceButtonTextParser.found) {
for (i = 0; i < objectToParse.children.length; i++) {
replaceButtonTextParser(objectToParse.children[i])
}
}
}
}
function replaceButtonText() {
replaceButtonTextParser.found = false;
if (messageDialogItem.enableButtonRenaming) {
replaceButtonTextParser(messageDialogItem.contentItem)
}
}
MessageDialog {
id: messageDialogItem
property bool enableButtonRenaming: false
property string okText: ""
property string openText: ""
property string saveText: ""
[...]
Component.onCompleted: {
replaceButtonText()
}
To copy to clipboard, switch view to plain text mode
Does anybody have an idea for a less badly written solution?
Bookmarks