PDA

View Full Version : MessageDialog with custom buttons



sedi
11th December 2016, 21:18
Hi,
with QMessageBox (http://doc.qt.io/qt-5/qmessagebox.html)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 (http://doc.qt.io/qt-5/qml-qtquick-dialogs-messagedialog.html) 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.contentI tem)
}
}

MessageDialog {
id: messageDialogItem
property bool enableButtonRenaming: false
property string okText: ""
property string openText: ""
property string saveText: ""
[...]
Component.onCompleted: {
replaceButtonText()
}


Does anybody have an idea for a less badly written solution?

anda_skoa
12th December 2016, 11:15
Hmm, I think implementing your own message box would be the cleanest solution.

Cheers,
_