Unsolved Q_Properties in a Qt program
-
That's exactly what I'm explaining.
event->region()
is the area that need to be repainted (the one under the notepad in my previous example). Ifrect
(which is 1 image pixel zoomed) is inside that region then repaint it, otherwise just skip it. You can actually remove that if altogether, it's there just to make the paint faster to avoid repainting regions that did not need repainting -
Thanks.
That if-condition is for "the whole" painting I think. I removed the condition and it made the parameter event be left uselessly! I can't accept that condition is useful.
About another if-condition:
if(event->buttons() & Qt::LeftButton)
in the mouseMoveEvent. Here it has used a bit-wise and (&
) while the operator==
will work too.Both sides are of the sate type (left, right or middle button). Why a bit-wise operator? (&)
And how could we convince ourselves that, that operator works correctly for that condition, please?
-
Hi
The bitwise & is used as the information is created that way. (using OR)
If multiple buttons were pressed, using == would fail even the Qt::LeftButton was actually also pressed.
So when a valued is created by using OR. the only correct way is to use & to check if that bit/bits is set. -
@tomy said in Q_Properties in a Qt program:
I removed the condition and it made the parameter event be left uselessly!
It's not uncommon to have useless arguments. There is even a macro
Q_UNUSED
to suppress warnings regarding useless parametersAnd how could we convince ourselves that, that operator works correctly for that condition, please?
think of
if
as "it executes the next block if the argument is not 0.event->buttons() & Qt::LeftButton
will return 0 only if theQt::LeftButton
bit is not set -
Thank you. But I still haven't got it.
The bitwise operators as their names express work on bits (0 and 1).
Do you mean that the statesevent->buttons()
andQt::LeftButton
return are actually bits?event->buttons()
returns a bit (0 or 1) andQt::LeftButton
also returns a bit (1 or 0). Then that bitwise operator&
, operates on the two states and returns the result according to the&&
rule? -
@tomy Please check the definition of http://doc.qt.io/qt-5/qt.html#MouseButton-enum, LeftButton has a value of 0x00000001
It is not a bit it is an integer.
If you do a bitwise & with for example 0x00000011 you will get 0x00000001 which is not 0 and as such true:0x00000011 & 0x00000001 = 0x00000001 // true 0x00000010 & 0x00000001 = 0x00000000 // false
This has nothing to do with Qt - C/C++ basics.
-
0x00000011 & 0x00000001 = 0x00000001 // true 0x00000010 & 0x00000001 = 0x00000000 // false
Thanks.
What's the criteria to consider it true/false? By the least-valuable bit?
I implemented the instructions for all three buttons.Qt::LeftButton 0x00000001
Qt::RightButton 0x00000002
Qt::MidButton 0x00000004Yes, they are integers and we will have one of those values if its corresponding button is pressed.
If we press "any" key (of the mouse) we will have a true value on the left side (event->buttons()). For instance, we press the midbutton. So on the left side we have a value true and on the right we have a value 4:true & 4 = 0x00000100 // true or false
Now is it interpreted true or false?
-
Nope, it's not converted to bool. Let's say you pressed both left and right mouse buttons.
event->buttons()
will returnQt::LeftButton | Qt::RightButton
which is 3 as an integer (suppose 32 bits)00000000000000000000000000000011
in binary. Now you "and" bit by bit withQt::LeftButton
which is 1 as an integer (suppose 32 bits)00000000000000000000000000000001
. The result is00000000000000000000000000000001
as an integer. now@VRonin said in Q_Properties in a Qt program:
think of if as "it executes the next block if the argument is not 0
since it's not 0 if executes the block
If it's still not clear: https://en.wikipedia.org/wiki/Bitwise_operation
-
I studied the bit-wise AND of that link.
Nope, it's not converted to bool.
Help says: event->buttons(): Returns the button state when the event was generated. The button state is a combination of Qt::LeftButton, Qt::RightButton, Qt::MidButton using the OR operator.
So by 'OR', it means 'plus' (+), not boolean OR!Thanks.
-
@tomy said in Q_Properties in a Qt program:
So by 'OR', it means 'plus' (+), not boolean OR!
It is a bitwise-OR. If you press left and right mouse buttons at the same time, buttons() will return
Qt::LeftButton | Qt::RightButton
==0x00000001 | 0x00000002
==0x00000003
.It is not 'plus' (+).
Qt::LeftButton | Qt::LeftButton
==0x00000001 | 0x00000001
==0x00000001
@tomy said in Q_Properties in a Qt program:
About another if-condition:
if(event->buttons() & Qt::LeftButton)
in the mouseMoveEvent. Here it has used a bit-wise and (&
) while the operator==
will work too.Both sides are of the sate type (left, right or middle button). Why a bit-wise operator? (&)
No,
==
can fail if you press two buttons at the same time. As above, if you press the left and right buttons simultaneously, buttons() will return0x00000003
.0x00000003 == Qt::LeftButton
evaluates to false0x00000003 & Qt::LeftButton
evaluates to true