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. QGraphicsScene remove item selectively
QtWS25 Last Chance

QGraphicsScene remove item selectively

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k Views
  • 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.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by gfxx
    #1

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

    scenet->setItemIndexMethod(QGraphicsScene::NoIndex);
    connect(ui->mybutton, SIGNAL(pressed()), this, SLOT(mycancelslot());
    
    ......
    
    void Mainwindows::mycancelslot()
    {
    QList<QGraphicsItem*> ListPix = scenet->items();
            scenet->removeItem(ListPix.last());
    }
    
    

    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();
                   item->setPixmap(QPixmap::fromImage(myimage));
                   item->setPos(mypoint);
                   scenet->addItem(item);
    }
    

    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

    regards
    giorgio

    bkt

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • gfxxG Offline
        gfxxG Offline
        gfxx
        wrote on last edited by
        #3

        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)))
                {
                    scene_Pallet->removeItem(citem[ct]);
                }
            }
        }
        

        I hope these help someone in future.

        regards
        Giorgio

        bkt

        1 Reply Last reply
        1

        • Login

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