How I can find out which widget particular was clicked?
-
Greetings .
I want to know as within of:
@
MyCustomWidget::mousePressEvent(QMouseEvent*)
MyCustomWidget::mouseReleaseEvent(QMouseEvent*)
MyCustomWidget::mouseMoveEvent(QMouseEvent*)
@How I can know on which widget the mouse event was generated? .
I understand a mouse event occurs when one or more mouse buttons are pressed or released inside a widget or when the mouse is moved .
I am creating a custom widget (using QWidget as the base class) which has several elements: an area that shows a picture (with a QScrollArea and QLabel) and an area with several checks (2 QPushButton, 1 QCheckBox, 1 QToolButton, ect) . I want to allow the user to highlight regions with the mouse on the image (drawing rectangles) , for this I am reimplementing the functions I wrote above.
The problem is that I only need do this when mouse events occur over the area shown in the image. So, my specific question is with QMouseEvent :
How I can find out which widget particular was clicked?
Formulating the question would be more generally:
When you have a complex custom widget (with several child widgets), How I can find out about which widget particular (from the children widgtes) mouse event has occurred?Thanks in advance for any help and/or suggestion.
-
Given the position from the mouse event, you should be able to use "a QWidget function called childAt":http://qt-project.org/doc/qt-5/qwidget.html#childAt-2 to get the child at that location.
The downside is that it returns only a single child, so if you've got a stack of children there, this will only give you the top-most (I believe).