Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Which button was released in eventFilter?

    General and Desktop
    3
    3
    722
    Loading More Posts
    • 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.
    • A
      Andaharoo last edited by

      Is there a function or something I can use to get which button was released when catching a button release event in eventFilter.

      IE: My widget has some child widgets that are filtered through the parent. This parent code does nothing because right and left button are always false.

      bool MyExample::eventFilter(QObject* object, QEvent* e)
      {
          if (e->type() == QEvent::MouseButtonRelease)
          {
              QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(e);
      	if (mouseEvent->buttons() == Qt::LeftButton)
      	    printf("Left\n");
              else if (mouseEvent->buttons() == Qt::RightButton)
                  printf("Right\n");
          }
      }
      

      Of course, I could keep track of the states myself but this seems like something Qt would be able to do and I can't figure out how.

      J.Hilk 1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        What you are doing is the right way to get which button (left or right) is released. What is the issue are you facing ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply Reply Quote 0
        • J.Hilk
          J.Hilk Moderators @Andaharoo last edited by

          @Andaharoo
          try it with mouseEvent->button() instead of buttons(), button() willö give you the button that caused the event, whereas 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. For mouse move events, this is all buttons that are pressed down. For mouse press and double click events this includes the button that caused the event. For mouse release events this excludes the button that caused the event.

          Pay attention to the last part
          For mouse release events this excludes the button that caused the event.

          You're filtering only for Release event -> you'll have to use button() function

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply Reply Quote 4
          • First post
            Last post