Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Proper way to propagate MouseEvent from parent to the specific child

Proper way to propagate MouseEvent from parent to the specific child

Scheduled Pinned Locked Moved Solved General and Desktop
qt6mouse eventevent handlingqml c++
2 Posts 1 Posters 416 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bibasmallB Offline
    bibasmallB Offline
    bibasmall
    wrote on last edited by bibasmall
    #1

    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 in currentPrimitive_. 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
    }
    
    1 Reply Last reply
    0
    • bibasmallB Offline
      bibasmallB Offline
      bibasmall
      wrote on last edited by bibasmall
      #2

      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 of mousePressEvent(e) .

      1 Reply Last reply
      0
      • bibasmallB bibasmall has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved