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. Remove QGraphicsEllipseItems

Remove QGraphicsEllipseItems

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 976 Views 2 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by hobbyProgrammer
    #1

    Hi,

    I would like to remove previously added ellipses, but I don't know how to do that.
    How can I find these items back so they can be removed?

    this is how I draw the ellipses:

    
    void graphicsView::addPoint(QPointF pos)
    {
        qv_points << pos;
        QGraphicsEllipseItem *newEllipse = new QGraphicsEllipseItem();
    
        double x,y;
        QPointF point = pos;
        x = point.x()-2.5;
        y = point.y()-2.5;
    
        newEllipse->setRect(QRectF(x,y,5,5));
        newEllipse->setBrush(QBrush(Qt::red, Qt::SolidPattern));
        newEllipse->setFlags(QGraphicsEllipseItem::ItemIsMovable | QGraphicsEllipseItem::ItemIsSelectable);
    
        //add item to scene
        scene->addItem(newEllipse);
    
        qDebug() << pos;
    }
    
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      The scene keeps a list of items.
      https://doc.qt.io/qt-5/qgraphicsscene.html#items

      Something like ( not compiled )
      Loop over all items.
      Try to cast it to a QGraphicsEllipseItem
      if cast is ok, it IS such item
      take it from scene
      delete it

      foreach (QGraphicsItem *item, scene()->items()) {
          QGraphicsEllipseItem*node = qgraphicsitem_cast<https://doc.qt.io/qt-5/qgraphicsscene.html#removeItem*>(item);
          if (!node) // it was NOT an QGraphicsEllipseItem item, so skip it
              continue;
            // its a QGraphicsEllipseItem
            scene()->removeItem(node); // take it out ( wont delete, see docs)
            delete node; // delete it
      }
          
      

      https://doc.qt.io/qt-5/qgraphicsscene.html#removeItem

      Notice the ownership comment.

      H 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        The scene keeps a list of items.
        https://doc.qt.io/qt-5/qgraphicsscene.html#items

        Something like ( not compiled )
        Loop over all items.
        Try to cast it to a QGraphicsEllipseItem
        if cast is ok, it IS such item
        take it from scene
        delete it

        foreach (QGraphicsItem *item, scene()->items()) {
            QGraphicsEllipseItem*node = qgraphicsitem_cast<https://doc.qt.io/qt-5/qgraphicsscene.html#removeItem*>(item);
            if (!node) // it was NOT an QGraphicsEllipseItem item, so skip it
                continue;
              // its a QGraphicsEllipseItem
              scene()->removeItem(node); // take it out ( wont delete, see docs)
              delete node; // delete it
        }
            
        

        https://doc.qt.io/qt-5/qgraphicsscene.html#removeItem

        Notice the ownership comment.

        H Offline
        H Offline
        hobbyProgrammer
        wrote on last edited by
        #3

        @mrjj hi thanks! I'll try this and if it works, I'll mark this as solved (and as the right answer)

        mrjjM 1 Reply Last reply
        0
        • H hobbyProgrammer

          @mrjj hi thanks! I'll try this and if it works, I'll mark this as solved (and as the right answer)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @hobbyProgrammer
          It should :)
          Please be aware that we cast it as the scene keeps all items as base pointers (QGraphicsItem *) so
          we have to check if right type.

          Sometimes if having many different types, you can also keep your own list to make it easier to access.

          H 1 Reply Last reply
          1
          • mrjjM mrjj

            @hobbyProgrammer
            It should :)
            Please be aware that we cast it as the scene keeps all items as base pointers (QGraphicsItem *) so
            we have to check if right type.

            Sometimes if having many different types, you can also keep your own list to make it easier to access.

            H Offline
            H Offline
            hobbyProgrammer
            wrote on last edited by
            #5

            @mrjj Hi, just realized that this will probably remove all EllipseItems, but I would like to remove them by position.
            So if I press the right mouse button, it gives the position to undoAction and then looks which item is placed there.

            mrjjM 1 Reply Last reply
            0
            • H hobbyProgrammer

              @mrjj Hi, just realized that this will probably remove all EllipseItems, but I would like to remove them by position.
              So if I press the right mouse button, it gives the position to undoAction and then looks which item is placed there.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @hobbyProgrammer
              Well yes it would if you do the same for all.

              But in that use case - you can use
              https://doc.qt.io/qt-5/qgraphicsscene.html#itemAt

              To ask for a item at some position which i guess you can get from the right click.

              H 1 Reply Last reply
              2
              • mrjjM mrjj

                @hobbyProgrammer
                Well yes it would if you do the same for all.

                But in that use case - you can use
                https://doc.qt.io/qt-5/qgraphicsscene.html#itemAt

                To ask for a item at some position which i guess you can get from the right click.

                H Offline
                H Offline
                hobbyProgrammer
                wrote on last edited by hobbyProgrammer
                #7

                @mrjj it works! thank you so much.

                for people watching this:

                you can change this:

                    QGraphicsEllipseItem*node = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
                

                into this:

                    QGraphicsPolygonItem*node = qgraphicsitem_cast<QGraphicsPolygonItem*>(item);
                

                and it will work for a polygon item

                or into this:

                    QGraphicsPixmapItem*node = qgraphicsitem_cast<QGraphicsPixmapItem*>(item);
                

                and it will work for pixmapItems
                etc.

                you can change this line into anything to remove all the object that are that type of graphicsItem

                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