Proper way to propagate MouseEvent from parent to the specific child
-
Hello, everyone!
I have a QQuickPaintedItem-based class, containing a collection of other QQuickPaintedItem-based items (it's a drawing field containing drawn elements, so each item in the collection is a child of the drawing field ). I want to propagate MouseEvent from parent to the exact child, let's say, the one stored incurrentPrimitive_
. What do I do to achieve my goal?WorkingArea::WorkingArea(QQuickItem* parent) : QQuickPaintedItem(parent), primitives_{ new Line(this), new Line(this) } //to illustrate which collection the class has { setFiltersChildMouseEvents(true); setAcceptedMouseButtons(Qt::AllButtons); } void WorkingArea::mousePressEvent(QMouseEvent* e) { //this code is called each time I click if (e->button() == Qt::RightButton) { //... some stuff e->accept(); return; } e->ignore(); } bool WorkingArea::childMouseEventFilter(QQuickItem* item, QEvent*) { //this code is never called if(item != currentPrimitive_) return true; return false; } //... Line::Line(QQuickItem* parent) : QQuickPaintedItem(parent) { setAcceptedMouseButtons(Qt::AllButtons); } void Line::mousePressEvent(QMouseEvent* e) { //this code is never called }
-
As I understand it, the child should receive the event before the parent, but in my case, the child is deaf. I thought maybe the problem was the size of the child, so I set it equal to the parent size, but it didn't help. I can call mousePressEvent(e) directly for the child in the parent's overload, but it looks like a very bad design.
UPD:
currentPrimitive_->setKeepMouseGrab(true);
allows to pass the event to the child without ugly direct call ofmousePressEvent(e)
. -