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) {
    void StateEditor::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *Event) {
        if (NameEditor->textInteractionFlags() == Qt::NoTextInteraction)

    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.

  • Lifetime Qt Champion


    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?

  • Lifetime Qt Champion

    Maybe a QGraphicsItemGroup ?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.