开发者

Mouse down on sub-widget, but which one?

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.


  1. You should write class (inherited from QObject) with reimplemented bool eventFilter(QObject *obj, QEvent *event); function.
  2. 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()).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜