Important: Please read the Qt Code of Conduct -

QGraphicsScene remove item selectively

  • hi ... I try to delete some QGraphicsPixmapItem from my scenet .....

    connect(ui->mybutton, SIGNAL(pressed()), this, SLOT(mycancelslot());
    void Mainwindows::mycancelslot()
    QList<QGraphicsItem*> ListPix = scenet->items();

    these delete the item on scenet but in unordered way .... eplain my better:

    in my app I add some items .... for last 5-15 QPixmapItem (user choose) ... under for statement:

    for(int i=0; i<InUserChose; i++)
           QGraphicsPixmapItem* item = new QGraphicsPixmapItem();

    at these point if i try to push "mybutton" the item is deleted, 1 item 1 button press OK, but disappear not only last QGraphicsPixmapItem, some other item too in unordered way ..... at last all item disappear & because my code is incomplete the app crash (I call removeitem but there is no item ... all deleted!!).

    Finally it works but not understand the QList<QGraphicsItem*> ListPix order .... using QGraphicsScene::BspTreeIndex is bad choice ....

    UPDATE: for remove last item use VRonin code as show in these post:
    VRonin solution for remove last item


  • The documentation on QGraphicsScene::items() explains how the list is ordered. This has nothing to do with the ItemIndexMethod.

  • I struggled a bit 'to understand online documents ... that's why I looked for help .... but in the end I did it:

    These code solve the problem (remove only the item with zvalue from 46 to 54):

    void MainWindow::test_selective_clean()
        QList<QGraphicsItem*> citem = scene_Pallet->items();
        for (int ct = 0; ct < citem.size(); ct++)
            if ((citem[ct]->zValue() > 45) && ((citem[ct]->zValue() < 55)))

    I hope these help someone in future.


Log in to reply