c++qtqgraphicsviewqgraphicsitem

QGraphicsItem position after changing boundingRect


I have class derived from QGraphicsItem. It contains vector of points which I draw in paint:

for(int i = 0; i < _vertexes.size(); i++)
{
     ...
  painter->drawEllipse(_vertexes[i], POINT_RADIUS, POINT_RADIUS);
}

when I add point in _vertexes with this code

 prepareGeometryChange();
 _vertexes.pop_back();

position of points in the view is changing, boundingRect is calculated using _vertexes

How to save points positions? I don't want all points change position after adding new one if new boundingRect is bigger. By the pos() returns always the same position (0, 0) but it could be in a different position of screen.


Solution

  • I don't set initial sceneRect, so it was recalculated and scrolled after each increasing scene items bounding rect. Setting scene rect by ui->graphicsView->setSceneRect(x, y, width, heigh); before adding of my items solves the problem,