Think of a window designed with Qt Designer. Lots of sub-widgets. T开发者_JS百科here is a QWidget derived class for this window which implements the virtual mouse methods of QWidget (mousePressEvent, mouseReleaseEvent, mouseMoveEvent)
Some of the sub-widgets are clean of controls, and are defined in the stylesheet as "background: transparent"
My question:
When such a transparent sub-widget is clicked, i do get the MousePressEvent called in my QWidget derived class. That's all fine. I do however would like to know WHICH sub-widget was just clicked. I'd like to avoid geometry comparisons to map the click location to the dimensions of my sub-widgets.
Is there a way to get this information from the mouseEvent object? I can't seem to see anything there related to the originator of the mouse event.
- You should write class (inherited from QObject) with reimplemented
bool eventFilter(QObject *obj, QEvent *event);
function. - You should install instance of this class as event filter in all needed subwidgets: http://doc.qt.io/qt-5/qobject.html#installEventFilter.
In reimplemented bool eventFilter(QObject *obj, QEvent *event);
function you can identify which subwidgets get MouseEvent first.
Call QWidget::childAt (event -> pos()).
精彩评论