Reputation: 703
I am drawing a polygon on qgraphicsscene using QPainterPath. However after sometime when I want to delete the path I am unable to delete it. Can someone tell me how to delete/erase the drawn path. I am trying the following code.
In header File:
QGraphicsScene *scene;
QGraphicsPixmapItem *m_pixItem;
QPainterPath m_pathTrack;
QPolygon m_qpolygon;
In cpp file
void MyClass::AddPath()
{
//Slot to add path to the scene
QImage l_img("Path");
graphicsView->setScene(&scene);
m_pixtemItem->setPixmap(QPixmap::fromImage(image));
//Code here to Adding points to polygon. The points are coming at regular interval
m_qpolygon.append(Point);
m_pathTrack.addPolygon(m_qpolygon);
scene.addPath(m_pathTrack);
}
// In slot to delete path
void MyClass::DeletePath()
{
//I tried doing this but the path does not erase
m_pathTrack = QPainterPath();
}
Thank You.
Upvotes: 2
Views: 1962
Reputation: 35
Remember to include the corresponding head file, QGraphicsItem. Or you will not able to call the function.
Upvotes: 0
Reputation: 2969
Just retain a pointer on QGraphicsPathItem
create when adding your path
QGraphicsPathItem* pathItem = scene.addPath(m_pathTrack);
Then you will be able to remove it from scene:
scene->removeItem(pathItem);
EDIT (credits to thuga):
If you don't plan to put again this item in your scene, you can free its memory once you have removed it from the scene.
scene->removeItem(pathItem);
delete pathItem;
Upvotes: 3