2014-06-23 185 views
1

我正在使用QPainterPath在qgraphicsscene上绘制一个多边形。但是,在某些时候,当我想要删除路径时,我无法将其删除。有人能告诉我如何删除/删除绘制的路径。 我正在尝试以下代码。从QGraphicsScene删除路径

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(); 
} 

谢谢。

回答

2

只是保留在QGraphicsPathItem指针创建加入你的路径

QGraphicsPathItem* pathItem = scene.addPath(m_pathTrack); 

时,然后你就可以从场景中删除它:

scene->removeItem(pathItem); 

EDIT(学分thuga)

如果您不打算在场景中再次放置此项目,则可以在您将其从场景中移除后释放其内存东北。

scene->removeItem(pathItem); 
delete pathItem; 
+1

如果不再需要,你必须记得删除它。 – thuga

+0

@thuga你是对的。我没有提到它,因为PathItem将被派往现场并且不会被泄漏。但我会编辑这个建议。谢谢:) – jbh

+0

感谢您的答复。我正在尝试这个,但它并没有删除绘制的整个路径。相反,几个初始点被删除。 – Sid411

0

答案似乎并没有有效的时刻 - 在Qt的5.8,QGraphicsScene:: addPath(*path)回报QGraphicsPathItem*QGraphicsScene::removeItem(*item)需要QGraphicsItem* item

有两种类型之间没有直接的转换。