Compilation error when trying to add code that involves OR'ing two MouseButtons



  • Hello,

    I am trying to perform a task whenever a mouse button is clicked. The problem is that I get a compilation error when I try to add in the code that involves more than 2 mouse buttons. Here is the snippet of my code:
    @
    using namespace Qt;
    .
    .
    .
    MouseButtons buttonState = mouseEvent->buttons();
    switch (buttonState)
    {
    case (LeftButton):
    mouseFunction = MouseTranslate;
    break;
    case (LeftButton | MidButton):
    mouseFunction = MouseDown;
    break;
    }
    @

    The error message I get is
    @
    error: calls to overloaded operators cannot appear in a constant-expression
    @
    and the error message specifically pointed to the line
    @
    case (LeftButton | MidButton):
    @

    I am unsure why this is so. I am able to print the result of LeftButton|MidButton through qDebug but when I put it in a case statement the compiler just doesn't like it. I get the same error message when I try OR'ing other buttons such as RightButton|LeftButton and MidButton|RightButton.

    I am unsure how to solve this problem. If anyone has any ideas what I'm doing wrong then I would be grateful if you offer it.
    Thanks in advance,
    Bob



  • The MouseButtons type is a typedef for QFlags<MouseButton>, so the arguments to the case statements must be of type QFlags<MouseButton> too. I'm not 100% sure, but it may be that the simple bitwise OR just results in an int, which would needed to be converted into the QFlags type.

    A better approach would be to test the flags with buttonState.testFlag(...). See the [[Doc:QFlags]] docs for details.

    BTW:
    Your logic looks a bit weird. The second case (LeftButton | MidButton) is only executed if both buttons are pressed simultaneousley! The expression does not catch the situation where either the left button or the mid button has been pressed (which is what I suppose it to do).



  • A work-around is to just use + instead of |. Note that that only works because LeftButton and MidButton are both simple 1-bit flags (all mouse buttons are). If you are dealing with more complex flags that might involve multiple bits, then using + obviously doesn't work.



  • Thank you guys for your replies. To Volker, sorry for not being clear but I only meant to bitwise-OR the two, not logically OR them.

    As for the problem, I managed to get around the problem by casting it as an int like so
    @
    case ((int)LeftButton | (int)MidButton):
    @

    Not really happy with the solution but it worked.



  • Bitwise ORing that two flags together yields an integer, that holds a value for LeftButton and MidButton together. Your case statement will only be executed if the event yields back that value, which would only happen if the user pressed the left and middle button simultaneously.

    The [[Doc:QFlags]] subclasses are not constructed to be used easily in switch statements. The testFlag() method would be a more elegant and reliable solution for you problem. You need to change your switch/case statement to a if/else if then.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.