Results 1 to 5 of 5

Thread: Order of buttons in QMessageBox

  1. #1
    Join Date
    Dec 2014
    Location
    Holland
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Order of buttons in QMessageBox

    Hello,
    I am just started with Qt Programming with help of the book "The Book of Qt4" by Daniel Molkentin.
    In chapter 4, there's an example of cuteeditor a simple texteditor.
    I use the example and when I try the next line:
    Qt Code:
    1. if (QMessageBox::question(this, tr("Save Document?"),
    2. tr("You want to create a new document, but the "
    3. "changes in the current document '%1' have not "
    4. "been saved. How do you want to proceed?"),
    5. tr("Save Document"), tr("Discard Changes") ))
    To copy to clipboard, switch view to plain text mode 
    The resulting Box:
    Screenshot from 2014-12-16 21:57:51.png
    The text "Save Document" is in the left button and the text "Discard Changes" in the right button. When I click on the right button, the save document dialog appears. It is easily to change the order of the text of the buttons, but how do I know what is the yes-button and what is the no-button. And second question: how can I check what's the return value of MessageBox::question for a button. It seems that is a boolean (yes button true and no button false?) but I cannot find a static method question of MessageBox with a boolean return value.
    Last edited by martin72; 16th December 2014 at 21:19.

  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: Order of buttons in QMessageBox

    This is one of the overloads listed here: https://qt-project.org/doc/qt-5-snap...-obsolete.html

    Cheers,
    _

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

    d_stranz (17th December 2014)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Order of buttons in QMessageBox

    The static QMessageBox::question() method returns an int value, which corresponds to which button was clicked, not a bool value. The code you have pasted above is incorrect - the last two arguments to the question() method are not strings, but a logical OR of QMessageBox::StandardButton flags and a single flag that indicates which is the default choice, respectively. If this code of Molkentin's compiles and runs, it's an accident or based on a QMessageBox method that is obsolete.

    (Edit: the method shown in the book is obsolete. Click this QMessageBox link, then click the "Obsolete members" link on the documentation web page). Beware of using old books to learn new Qt...

    So the correct code should read:

    Qt Code:
    1. if (QMessageBox::Save == QMessageBox::question(this, tr("Save Document?"),
    2. tr("You want to create a new document, but the "
    3. "changes in the current document '%1' have not "
    4. "been saved. How do you want to proceed?"),
    5. QMessageBox::Save | QMessageBox::Discard, QMessageBox::Save ))
    6. {
    7. // save the document
    8. }
    9. else
    10. {
    11. // don't save the document
    12. }
    To copy to clipboard, switch view to plain text mode 

    As far as I know, it is not possible to change the order of buttons in the static QMessageBox methods. The order is in part set by the conventions used in the operating system where the program is running and which buttons are chosen for use.
    Last edited by d_stranz; 17th December 2014 at 00:23.

  5. The following user says thank you to d_stranz for this useful post:

    martin72 (17th December 2014)

  6. #4
    Join Date
    Dec 2014
    Location
    Holland
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Order of buttons in QMessageBox

    D_stranz, thank you for your answer. This makes it very clear to me. The way it is used in this book is indeed an obsolete method and I think it was by accident that after exchanges the text of the buttons it works.
    I tried your solution and it works fine, see the figure:
    Screenshot from 2014-12-17 22:12:36.png
    As you see the Discard button has a default text: close without saving.

    I am learning Qt with this book because I thought this book is useful to learn the basics of Qt 4.8 and besides some errors, this book is very helpful for me. After this book I will start with Qt 5.4. Do you know a good book for learning this latest version and some advanced topics?

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Order of buttons in QMessageBox

    After this book I will start with Qt 5.4. Do you know a good book for learning this latest version and some advanced topics?
    I do not know if any Qt author has updated their book for Qt 5. The best thing is to use the book you have, but refer to the Qt 5 documentation when something does not work the way you expect it to. Most of the core parts of Qt 4.8 have not changed very much in Qt 5, so almost all programs written for Qt 4.8 will work without changes in Qt 5.

    The biggest change is that in Qt 5, the include and library file names and locations have been rearranged. (For example, all QWidget-based classes are now under QtWidgets instead of QtGui). Another big change is that OS-dependent code has been moved into plugins (qwindows.dll for Windows, for example). This changes the way you create and deploy installations, if you get to that point.

    I generally like the books by Blanchette and Summerfield ("C++ GUI Programming with Qt 4") and Summerfield ("Advanced Qt Programming" and "Rapid GUI Programming with Python and Qt"). I have Molkentin's book also, as well as Ezust and Ezust ("An Introduction to Design Patterns in C++ with Qt 4"), but I don't use these books very much.

    Summerfield's Advanced Qt book is very good for learning about the Model / View and Graphics / View architectures as well as other more advanced topics.

Similar Threads

  1. QMessageBox - Text in the buttons
    By graciano in forum Qt Programming
    Replies: 4
    Last Post: 20th June 2015, 23:51
  2. QMessageBox custom buttons icons
    By asik in forum Newbie
    Replies: 2
    Last Post: 2nd December 2010, 16:37
  3. tab order not working properly for radio buttons
    By netmat in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2010, 14:58
  4. QMessageBox buttons appearance
    By yartov in forum Qt Programming
    Replies: 6
    Last Post: 26th June 2008, 01:36
  5. Replies: 3
    Last Post: 25th April 2007, 12:43

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.