Results 1 to 2 of 2

Thread: MessageDialog with custom buttons

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default MessageDialog with custom buttons

    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:

    Qt Code:
    1. QtObject {
    2. id: priv
    3. property string okStandard: "OK"
    4. property string openStandard: "Öffnen"
    5. property string saveStandard: "Speichern"
    6. [...]
    7. }
    8.  
    9. function replaceButtonTextParser(objectToParse) {
    10. if (!replaceButtonTextParser.found) {
    11. for (var i = 0; i < objectToParse.children.length; i++) {
    12. if (objectToParse.children[i].text !== "") {
    13. replaceButtonTextParser.found = true;
    14. if (messageDialogItem.okText !== "" && objectToParse.children[i].text === priv.okStandard) {
    15. objectToParse.children[i].text = messageDialogItem.okText
    16. } else if (messageDialogItem.openText !== "" && objectToParse.children[i].text === priv.openStandard) {
    17. objectToParse.children[i].text = messageDialogItem.openText
    18. } else [if ... else] {
    19. replaceButtonTextParser.found = false;
    20. }
    21. }
    22. }
    23. if (!replaceButtonTextParser.found) {
    24. for (i = 0; i < objectToParse.children.length; i++) {
    25. replaceButtonTextParser(objectToParse.children[i])
    26. }
    27. }
    28. }
    29. }
    30.  
    31. function replaceButtonText() {
    32. replaceButtonTextParser.found = false;
    33. if (messageDialogItem.enableButtonRenaming) {
    34. replaceButtonTextParser(messageDialogItem.contentItem)
    35. }
    36. }
    37.  
    38. MessageDialog {
    39. id: messageDialogItem
    40. property bool enableButtonRenaming: false
    41. property string okText: ""
    42. property string openText: ""
    43. property string saveText: ""
    44. [...]
    45. Component.onCompleted: {
    46. replaceButtonText()
    47. }
    To copy to clipboard, switch view to plain text mode 

    Does anybody have an idea for a less badly written solution?
    Last edited by sedi; 12th December 2016 at 02:33. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MessageDialog with custom buttons

    Hmm, I think implementing your own message box would be the cleanest solution.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sedi (13th December 2016)

Similar Threads

  1. MessageDialog does not work on iOS
    By Urthas in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2016, 20:37
  2. Custom look for buttons
    By harvey_slash in forum Newbie
    Replies: 0
    Last Post: 3rd October 2013, 14:18
  3. How to custom Buttons in QColorDialog
    By picobsd in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2011, 11:37
  4. custom shaped buttons
    By jsabater in forum Qt Programming
    Replies: 5
    Last Post: 2nd July 2009, 15:17
  5. Custom buttons problem
    By giotto in forum Qt Programming
    Replies: 7
    Last Post: 7th February 2008, 22:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.