Which button was released in eventFilter?
-
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.
-
What you are doing is the right way to get which button (left or right) is released. What is the issue are you facing ?
-
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.
@Andaharoo
try it withmouseEvent->button()
instead of buttons(), button() willö give you the button that caused the event, whereas buttonsReturns 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