Skip to content Skip to sidebar Skip to footer

Values Of QMessageBox.Yes/QMessageBox.No

I learn PyQt5 (which looks quite complicated for me) and I am wondering why QMessageBox.Yes(or no) has some special value, for me it is 16384. That is what I mean: from PyQt5 impo

Solution 1:

Because yes and no are not the only options. What if you wanted yes, no, cancel and abort (which all had different meanings for your application)? You can't represent those with a single Boolean.

So instead it is common practice to use an integer. Of course remembering what each integer means is annoying, so they are stored in nicely named variables so your code stays readable!

There is nothing inherently special about the specific values used, other than the fact that they are powers of 2 (2^14 and 2^16 respectively) which allows they to be combined into a single integer using the "bitwise or" operation and then later extracted from the resultant integer using "bitwise and". This is a common way of combining option flags into a single variable so you don't need to add a new argument to a function/method every time you add an option (important in languages where optional method arguments can't be done). If you weren't aware, you already used the bitwise or to combine the flags in the message box constructor!


Solution 2:

If we look inside the QT documentation, we can read the following for the QMessageBox.question method's return value:

Returns the identity of the standard button that was clicked.

The identity for every standard button is an integer number, which is also specified by the documentation:

QMessageBox::Yes 0x00004000 QMessageBox::No 0x00010000

If we convert this hex values to decimals, we get 16384 and 65536.

why not True or False?

Because there are a lot more options here, there are a lot more standard buttons (ex. Ok, Open, Save, Cancel, etc.)

For more information you should read the documentation for QT: http://doc.qt.io/qt-5/qmessagebox.html (yes, it's for C++ but it is also applicable for the python wrapper)


Post a Comment for "Values Of QMessageBox.Yes/QMessageBox.No"