QGraphicsItem: Notifying parent of changes to boundingRect
-
wrote on 6 Jul 2016, 10:05 last edited by BjornW 7 Jun 2016, 15:09
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
-
wrote on 6 Jul 2016, 10:20 last edited by A Former User 7 Jun 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.
-
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.
wrote on 6 Jul 2016, 13:03 last edited by@Wieland
That's a good idea, will do. Still, is there any built-in way to get a notification when the boundingRect changes?
-
@Wieland
That's a good idea, will do. Still, is there any built-in way to get a notification when the boundingRect changes?
wrote on 6 Jul 2016, 13:17 last edited by@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.
-
@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.
wrote on 6 Jul 2016, 13:28 last edited by@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.
4/5