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. QGraphicsItem: Notifying parent of changes to boundingRect
QtWS25 Last Chance

QGraphicsItem: Notifying parent of changes to boundingRect

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsitem
5 Posts 2 Posters 2.2k 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.
  • B Offline
    B Offline
    BjornW
    wrote on 6 Jul 2016, 10:05 last edited by BjornW 7 Jun 2016, 15:09
    #1

    Hi!

    I'm working with a QGraphicsScene / QGraphicsItems and is trying to create a container class that draws an outline around a number of child items,

    class MyContainer : public QGraphicsItem
    {
    public:
       QRectF boundingRect() const override
       {
          return m_boundingRect;
       }
       void notifyMoved(MyItem* )
       {
          updateBoundingRect();
       }
       void updateBoundingRect()
       {
    	prepareGeometryChange();
        ... //compute new bounding rect containing all the item's bounding rects
       }
       ...
    private:
       QRectF m_boundingRect;
       ...
    };
    

    and

    class MyItem: public QGraphicsItem
    {
    public:
       MyItem(MyContainer* pContainer) 
          : QGraphicsItem(pContainer), m_pContainer(pContainer) {}
       QVariant itemChange(GraphicsItemChange change, const QVariant & value) override
       {
          //notify container if this item moved
          ...
          m_pContainer->notifyMoved(this);
          ...
       }
       ...
    private:
       MyContainer* m_pContainer;
    };
    

    (Not the exact code but this is how it works in principle)

    This works great as long as the items move. Notifications are sent and the container rect (and graphics) are updated properly. However, I run into problems when I change the bounding rect of a contained item. MyItem does not know when its bounding rect changes. How can I inform it? Is there a standard way to do this? Something like this would be useful:

    QGraphicsItem::ItemBoundingRectHasChanged
    

    I could definitely write my own code that notifies whenever i set the bounding rect, but this would get tedious since MyItem may actually be any kind of subclass to MyItem that handles its bounding rect in its own way. I would like to do it "the Qt way"!

    Any ideas are welcome! :-)

    //Björn W

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 6 Jul 2016, 10:20 last edited by A Former User 7 Jun 2016, 10:20
      #2

      Hi, welcome to the Qt forum! If you need graphics items with signals and slots then don't derive MyItem from QGraphicsItem but from QGraphicsObject instead. The rest should be trivial.

      B 1 Reply Last reply 6 Jul 2016, 13:03
      0
      • ? A Former User
        6 Jul 2016, 10:20

        Hi, welcome to the Qt forum! If you need graphics items with signals and slots then don't derive MyItem from QGraphicsItem but from QGraphicsObject instead. The rest should be trivial.

        B Offline
        B Offline
        BjornW
        wrote on 6 Jul 2016, 13:03 last edited by
        #3

        @Wieland

        That's a good idea, will do. Still, is there any built-in way to get a notification when the boundingRect changes?

        ? 1 Reply Last reply 6 Jul 2016, 13:17
        0
        • B BjornW
          6 Jul 2016, 13:03

          @Wieland

          That's a good idea, will do. Still, is there any built-in way to get a notification when the boundingRect changes?

          ? Offline
          ? Offline
          A Former User
          wrote on 6 Jul 2016, 13:17 last edited by
          #4

          @BjornW No, there is no built-in way. Standard graphics items aren't derived from QObject to keep them lightweighted, thus they don't emit any signals.

          B 1 Reply Last reply 6 Jul 2016, 13:28
          0
          • ? A Former User
            6 Jul 2016, 13:17

            @BjornW No, there is no built-in way. Standard graphics items aren't derived from QObject to keep them lightweighted, thus they don't emit any signals.

            B Offline
            B Offline
            BjornW
            wrote on 6 Jul 2016, 13:28 last edited by
            #5

            @Wieland I was thinking about

            QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant & value)
            

            or similar. That does not use the signal/slot system.

            Anyway, gonna change my code to use slots.

            1 Reply Last reply
            0

            4/5

            6 Jul 2016, 13:17

            • Login

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