QT, repaint line that doesn't have boundingRect()

  • 0_1513739237721_Untitled.png
    There are 3 items in the picture above.

    1. Yellow Rect : QAbstractGraphicsShapeItem
    2. Yellow Point : QAbstractGraphicsShapeItem
    3. Two white lines : just two lines connecting Yellow Rect and Yellow Point

    Yellow Rect and Yellow Point are QAbstractGraphicsShapeItem which has boundingRect() and shape() functions that can detect the location of Rect and Point, so it's working good when I move these two items.

    The two white lines are simply two lines connecting the Rect and Point, when I move point or rect, the lines are supposed to move immediately so it keeps the connections between Rect and Point. However, since two white lines are not in the boundRect area of Yellow Rect and Yellow Point, the two while lines won't update immediately.

    There are two solutions below here, but not ideal, I am looking for better solution.

    1. paint the whole picture whenever Rect and Point moves, two white lines will also re-paint. But I don't want to re-paint the whole picture which contains many other graphics, which is time consuming and not smart.

    2. add the area of two white lines to the BoundingRect() or Shape() of either Yellow Rect or Yellow Point, then two white lines will re-paint immediately since it's part of Rect or Point. But not good, if I drag two white lines, Rect or Point will also be selected and dragged, which is not what I need. The two white lines should never be selected, it's sole purpose is to visually connect Yellow Rect and Yellow Point.

    Any better solutions? Thanks in advance.

  • Lifetime Qt Champion

    I just keep a pointer to the "white lines" and adjust if i move yellow.

    class ColorItem : public QGraphicsItem {
      ConnectorLine* line = nullptr;
      QVariant itemChange(GraphicsItemChange change, const QVariant& value) override {
        if (change == ItemPositionChange && scene()) {
          if (line) {
        return QGraphicsItem::itemChange(change, value);
      QColor color;

    line->Adjust() alters its endpoints. You could just adjust it directly here.