2
votes

I am trying to capture the mouse release event in a widget ensuring that the mouse was previously pressed in same widget. mouseReleaseEvent is successfully called but I don't know what to do with the parameter QMouseEvent to check if the position of the mouse is inside the widget. My current code:

void DeviceWidget::mouseReleaseEvent(QMouseEvent* e)
{
    if (_mouseClick)
    {
        _mouseClick = false;
        emit mouseClick(_deviceInformation);
    }
}

Thank you so much,

1
This event should only be triggered if the mouse was on the widget when it was released. Checking for it is superfluous.RedX
Not, it's triggered even if I release the mouse in another place such as the desktop.Didac Perez Parera
The release event is reported to the same widget that had the press event. You should do nothing special about it. The actual pointer position at the time of release is irrelevant.n. 1.8e9-where's-my-share m.

1 Answers

5
votes

You need to check if the local position of the mouse release event is within your widget. I don't think there's a reason to use the _mouseClick member variable. This method will only be called if the mouse press was on this widget. When you press the button, the widget starts tracking the mouse, and it will receive the release event - no matter where the mouse was released.

void DeviceWidget::mouseReleaseEvent(QMouseEvent* e)
{
    if (rect().contains(e->localPos()->toPoint()) {
        emit mouseClick(_deviceInformation);
    }
}