Unsolved Updating QGraphicsItem when editing text of containing QGraphicsTextItem
-
I am trying to experiment with QGraphicsTextItem that is a child of a QGraphicsItem. The QGraphicsItem draws a rectangle around the QGraphicsTextItem and the idea is that this rectangle resizes with each change to the text of the QGraphicsTextItem. I use the mouseDoubleClicked event on the QGraphicsItem to make the QGraphicsTextItem editable and the focusOutEvent on the QGraphicsItem to make the QGraphicsTextItem no longer editable. The code for that looks as follows (where NameEditor is the QGraphicsTextItem and StateEditor is the QGraphicsItem):
void StateEditor::focusOutEvent(QFocusEvent *Event) { NameEditor->setTextInteractionFlags(Qt::NoTextInteraction); QGraphicsItem::focusOutEvent(Event); } void StateEditor::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *Event) { if (NameEditor->textInteractionFlags() == Qt::NoTextInteraction) NameEditor->setTextInteractionFlags(Qt::TextEditorInteraction); QGraphicsItem::mouseDoubleClickEvent(Event); }
My question is however: how can I redraw the QGraphicsItem with each modification of the text in the QGraphicsTextItem? In particular, when its boundingbox becomes smaller, the area of the old bounding box should be redrawn as well.
-
Hi,
Out of curiosity, why not paint the rectangle directly in your custom QGraphicsTextItem ?
-
Good point. Didn't think about that. However, I intend to make the QGraphicsItem more complex in the future with a second QGraphicsTextItem... So, for that case I guess I need to do it in the QGraphicsItem. Or can this be done also differently?
-
Maybe a QGraphicsItemGroup ?