2014-06-23 2 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(); 
} 

감사합니다.

scene->removeItem(pathItem); 

EDIT (thuga 학점) : 그런 다음 장면에서 제거 할 수있을 것입니다 경로

QGraphicsPathItem* pathItem = scene.addPath(m_pathTrack); 

를 추가 할 때

답변

2

그냥 만들 QGraphicsPathItem에 대한 포인터를 유지

장면에이 항목을 다시 넣으 려하지 않으면 sce에서 제거한 후에도 메모리를 해제 할 수 있습니다 네. , Qt는 5.8 QGraphicsPathItem*QGraphicsScene::removeItem(*item)QGraphicsItem* item 소요 QGraphicsScene:: addPath(*path) 수익률

두 종류의 사이에 직접 변환은 없습니다 -

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

더 이상 필요하지 않은 경우에도 삭제해야합니다. – thuga

+0

@thuga 당신 말이 맞아요. 나는 pathItem이 장면에 부모 역할을하여 유출되지 않을 것이라고 언급하지 않았다. 그러나 나는이 충고를 편집 할 것이다. 감사합니다 :) – jbh

+0

답장을 보내 주셔서 감사합니다. 나는 이것을 시도하고 있지만 그려지는 전체 경로를 삭제하지는 않는다. 대신 초기 지점 몇 개가 삭제됩니다. – Sid411

0

대답은 순간에 유효하지 않는 것 같습니다..