QT, repaint line that doesn't have boundingRect()
-
There are 3 items in the picture above.- Yellow Rect : QAbstractGraphicsShapeItem
- Yellow Point : QAbstractGraphicsShapeItem
- 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.
-
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.
-
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.
-
Hi
I just keep a pointer to the "white lines" and adjust if i move yellow.class ColorItem : public QGraphicsItem { .. ConnectorLine* line = nullptr; .. protected: QVariant itemChange(GraphicsItemChange change, const QVariant& value) override { if (change == ItemPositionChange && scene()) { if (line) { line->adjust(); } } return QGraphicsItem::itemChange(change, value); } private: QColor color; };
line->Adjust() alters its endpoints. You could just adjust it directly here.