Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/machine-learning/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 从QGraphicscene中删除路径_Qt_Qgraphicsscene - Fatal编程技术网

Qt 从QGraphicscene中删除路径

Qt 从QGraphicscene中删除路径,qt,qgraphicsscene,Qt,Qgraphicsscene,我正在使用QPainterPath在Qgraphicscene上绘制多边形。但是,过了一段时间,当我想删除路径时,我无法删除它。有人能告诉我如何删除/删除绘制的路径吗。 我正在尝试以下代码 In header File: QGraphicsScene *scene; QGraphicsPixmapItem *m_pixItem; QPainterPath m_pathTrack; QPolygon m_qpolygon; In cpp file void MyClass::A

我正在使用QPainterPath在Qgraphicscene上绘制多边形。但是,过了一段时间,当我想删除路径时,我无法删除它。有人能告诉我如何删除/删除绘制的路径吗。 我正在尝试以下代码

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

谢谢。

添加路径时,只需在
QGraphicsPathItem
create上保留一个指针即可

QGraphicsPathItem* pathItem = scene.addPath(m_pathTrack);
然后,您将能够将其从场景中移除:

scene->removeItem(pathItem);
编辑(归功于thuga)

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

scene->removeItem(pathItem);
delete pathItem;

记住要包含相应的头文件QGraphicsItem。
或者您将无法调用该函数。

如果不再需要它,您也必须记住将其删除。@thuga您是对的。我没有提到它,因为pathItem将作为场景的父项,并且不会泄漏。但我会编辑这个建议。谢谢你的回复。我正在尝试,但它并没有删除绘制的整个路径。相反,一些初始点被删除了。我知道了。谢谢你的帮助。我稍微改变了一下实现。我使用scene.addItem()添加路径,然后使用pathItem.setPath()和setPen。再次感谢:)您通过
访问一次场景和通过
->
访问一次场景有什么原因吗?在尝试实现您的解决方案时,我还注意到一个错误:“参数1从'QGraphicsPathItem*'到'QGraphicsItem*'的转换未知”。我错过什么了吗?