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. [Solved] QGraphicsObject with child groups mousePressEvent not firing
Forum Updated to NodeBB v4.3 + New Features

[Solved] QGraphicsObject with child groups mousePressEvent not firing

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.8k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mirswith
    wrote on last edited by
    #1

    After lots of googling and testing I am stuck. I have a derived QGraphicsObject that contains a number of QGraphicsGroup's and QGraphicsItem's and for the life of me I am unable to get the mousePressEvent to fire for my object, interestingly enough my objects boundRect() does get called when I click the graphics view.

    The constructor of my QGraphicsItem looks something like this:

    @
    MyItem::MyItem(QGraphicsItem * parent) : QGraphicsObject(parent)
    {
    setFlags(QGraphicsItem::ItemIsSelectable);
    setHandlesChildEvents(true);

    _root = new QGraphicsItemGroup();
    _root->setParentItem(this);
    
    QGraphicsRectItem * item = new QGraphicsRectItem();
    item->setRect(0, 0, 100, 100);
    _root->addToGroup(item);
    

    }
    @

    I continue to add many more items but for readability lets assume I just have a rectangle that is 100x100. Next I override the boundRect() function:

    @
    QRectF MyItem::boundingRect() const
    {
    return QRectF(-50, -50, 50, 50);
    }
    @

    My object is being painted correctly but I simply do not get mouse events. I am using the default QGraphicsScene and QGraphicsView. I have tried all the combinations of setting setHandlesChildEvents on myself and _root, but that does not seem to make a difference. Any ideas?

    Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Could you show your declaration of your mouse event handler?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mirswith
        wrote on last edited by
        #3

        Definitely!

        @
        void MyItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
        {
        if( event->button()==Qt::LeftButton )
        {
        // do something
        }
        }
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mirswith
          wrote on last edited by
          #4

          Ok, this is strange. I created a test app thinking I would post it here to demonstrate the problem however in my test app it works great so clearly it is something I am doing wrong. I'll post my solution once I find one here.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mirswith
            wrote on last edited by
            #5

            Found my problem. I was adding MyItem objects to a group and then adding that group to my scene, turns out if I set setHandlesChildEvents to false on my group then it allows MyItem to get the mouse events as normal.

            Hope this is helpful.

            1 Reply Last reply
            0

            • Login

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