Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Compilation error when trying to add code that involves OR'ing two MouseButtons
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bobby B
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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).

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bobby B
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved