Remove Single Elements / QLineF from QGraphicsScene

I drew a grid in qgraphicsscene with QLineF , and you can add custom qgraphicsitems to the scene as blocks in a grid. I want to be able to redraw the grid at different intervals and indexing (indexes are just thicker rows).

My problem is that I do not know how to simply delete the lines. I can remove everything from the scene, but it takes too much work, because I need to copy all my custom elements into an array, and then add them to the scene again. I tried to make a list of pointers in strings, but I cannot delete pointers. I also thought about adding rows as children to qgraphicsitem and deleting that element, which will delete all rows, but you cannot set parents to rows.

How to remove certain line components from qgraphicsscene ?

Like this: scene->removeItem(..pointer..); I incorrectly searched for the result of scene->addItem(..) as an object, and then saved it and the link. When I save the result as a pointer, I was able to manipulate the element again.

+4
source share
1 answer

You can use QGraphicsItemGroup - http://qt-project.org/doc/qt-4.8/qgraphicsitemgroup.html , something like ...

 void Grid::addVerticalLineAt(qreal xCoord) { QRectF sceneRect = scene()->sceneRect(); QGraphicsLineItem* line = scene()->addLine(xCoord, mapFromScene(sceneRect.top()), xCoord, mapFromScene(sceneRect.bottom())); (QGraphicsItemGroup*)(this->gridLines)->addToGroup(line); } void Grid::Refresh(qreal p_Scale) { delete this->gridLines; //deletes all lines underneath gridLines = new QGraphicsItemGroup(this); //construct this _before_ you start calling addXXXLineAt qreal spacing = BASIC_SPACING * p_Scale; for(qreal curXCoord = sceneRect.left(); curXCoord < sceneRect.right(); curXCoord += spacing) //each spaced point in scenerect { addVerticalLineAt(curXCoord); } } 

... must work.

Another approach would be to simply add another QGraphicsItem called Grid and assign all the QLineF for this element by setting the parent in the QGraphicsLineItem ctor.

+1
source

Source: https://habr.com/ru/post/1434887/


All Articles