Problems with Open-Source Downloads read and

Mass removing of QGraphicsLineItem's from a QGraphicsScene

  • I handle graph's. I have a QGraphView connected to a QGraphicsScene. Some hundreds of constant header rectangles +texts are added first to the scene, then the graph containing abt. 5000 short linesegments. While time by time different graph's have to be drawn on the same base scene, I have created a QGraphicsItemGroup, and all the later-to-remove linesegments are added to this itemgroup. When I have to redraw with another graph, I empty the group, then add the new segments. It works well -except that emptying the itemgroup is very slow (takes seconds). Repopulate is much faster (<100msec), deleting some thousand QGraphicsLineItem's is the time-comsuming part. How to re-structure to make it faster?
    (How to create a "mark" of actual state of the scene and later restore to this point (==remove multiple QGraphicsItem's from the QGraphicsScene) without memory leaks?

    @QGraphicsScene* sc;
    QGraphicsItemGroup *scig1;

    @ QTime t; //for check timings
    QGraphicsItem *del;
    while (scig1->childItems().count() > 0) { //deleting old segments
    del= scig1->childItems().at(0);
    // del= scig1->childItems().at(scig1->childItems().count()-1); //deleting from back or forth -no difference
    delete del;
    qDebug() << QString("after zap %1").arg(t.elapsed());


    other try:

    scig1= sc->createItemGroup(QList<QGraphicsItem*>::QList() );
    qDebug() << QString("graf törlés után %1").arg(t.elapsed());

  • Why you don't just delete a group (it will delete children) and then create new one?

  • I couldn't find fast solution on the itemgroup way. Emptying and re-populationg itemgroup, or either deleting and re-creating was too slow (10,000 elements take 5 seconds on a strong quadcore pc w/ radeon 5670)
    The solution seems to be a single QGraphicsPathItem object added to scene once. At every redraw I create a new QPainterPath, add all the segments, then assign this QPainterPath to the QGraphicsPathItem. This is very fast (the same couple thousand elements are redrawn in 15msec!!)

    @QGraphicsScene sc;
    QGraphicsPathItem graf;
    void MyGrView::DiagramRepaint(void) {
    QPainterPath lin;
    lin.moveTo( Pt->data()[0]->x, Pt->data()[0]->y );
    for (int i=1; i < Pt->NrPoints; i++) {
    lin.lineTo( Pt->data()[i]->x, Pt->data()[i]->y );

Log in to reply