Detecting when a QGraphicsItem's boundingRect has changed
-
I am looking for a clean way to detect when the boundingRect of a QGraphicsItem subclass has changed, or is about to change.
What I have found so far:
-) prepareGeometryChange is not virtual, therefore I should not subclass it (why, oh why is that one not virtual!?)
-) The itemChange event has everything BUT the boundingRect. I could track a change in position, but if only the size changes, I wouldn't get an event, according to the docsOther ideas?
-
You are the only one that change the boundingRect (calling prepareGeometryChange), so after you call prepareGeometryChange you could call an other function that does all what you want.
-
For a QGraphicsTextItem, I doubt that. It automatically seems to enlarge depending on how much text you put into it.
-
Hmmm... I would have expected that
setting the QGraphicsItem::ItemSendsGeometryChanges flag on your graphics item, and
reimplement the itemChange() method of your QGraphicsItem
would do the trick, but it seems there is no QGraphicsItem::ItemSizeHasChanged value available. Weird... Good question!
-
If your item is a QGraphicsTextItem, its easy; every QGraphicsTextItem have a QTextDocument, so you can connect the signal "contentsChanged()":http://doc.qt.nokia.com/4.7-snapshot/qtextdocument.html#contentsChanged with a slot that do what you want.
@
QObject::connect(QGraphicsTextItem::document(), SIGNAL(contentsChanged()),
this, SLOT(doSomething()));
@[quote author="Asperamanca" date="1341213416"]For a QGraphicsTextItem, I doubt that. It automatically seems to enlarge depending on how much text you put into it.
[/quote] -
Note that, while that may serve the purpose of the TS, it is not a real solution. A new text might have the same size, after all.
-
Mmm I dont understand what TS mean. We want to detect when boundingRect has changed does not matter if the boundingRect has the same size. Or yes?
-
@Mario: Thanks for the hint regarding QGraphicsTextItem. It might be a useful workaround, if nothing comes up that works for all QGraphicsItem subclasses.