Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mass removing of QGraphicsLineItem's from a QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Mass removing of QGraphicsLineItem's from a QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    novaktamas
    wrote on last edited by
    #1

    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;
    t.start();
    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
    scig1->removeFromGroup(del);
    delete del;
    }
    qDebug() << QString("after zap %1").arg(t.elapsed());

    @

    other try:

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MarekR22
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • N Offline
        N Offline
        novaktamas
        wrote on last edited by
        #3

        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;
        ...
        sc.addItem(&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 );
        }
        graf.setPath(lin);
        }
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved