PDA

View Full Version : Order of buttons in QMessageBox



martin72
16th December 2014, 22:12
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:


if (QMessageBox::question(this, tr("Save Document?"),
tr("You want to create a new document, but the "
"changes in the current document '%1' have not "
"been saved. How do you want to proceed?"),
tr("Save Document"), tr("Discard Changes") ))

The resulting Box:
10812
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.

anda_skoa
17th December 2014, 01:01
This is one of the overloads listed here: https://qt-project.org/doc/qt-5-snapshot/qmessagebox-obsolete.html

Cheers,
_

d_stranz
17th December 2014, 01:16
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:



if (QMessageBox::Save == QMessageBox::question(this, tr("Save Document?"),
tr("You want to create a new document, but the "
"changes in the current document '%1' have not "
"been saved. How do you want to proceed?"),
QMessageBox::Save | QMessageBox::Discard, QMessageBox::Save ))
{
// save the document
}
else
{
// don't save the document
}


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.

martin72
17th December 2014, 22:28
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:
10821
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?

d_stranz
18th December 2014, 03:39
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.