Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QGraphicsScene remove item selectively

    General and Desktop
    2
    3
    799
    Loading More Posts
    • 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.
    • gfxx
      gfxx last edited by gfxx

      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 Reply Quote 0
      • A
        Asperamanca last edited by

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

        1 Reply Last reply Reply Quote 0
        • gfxx
          gfxx last edited by

          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 Reply Quote 1
          • First post
            Last post