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. Which button was released in eventFilter?
QtWS25 Last Chance

Which button was released in eventFilter?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.2k 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.
  • A Offline
    A Offline
    Andaharoo
    wrote on last edited by
    #1

    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.HilkJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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
      0
      • A Andaharoo

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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


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

        1 Reply Last reply
        4

        • Login

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