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 QGraphicsTextItem from QGraphicsScene
QtWS25 Last Chance

Remove QGraphicsTextItem from QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicssceneremovetextitemqgraphicstextit
7 Posts 2 Posters 2.5k 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.
  • D Offline
    D Offline
    deleted396
    wrote on last edited by
    #1

    Hello! I am showing numbers with QGraphicsTextItem in a QGraphicsSene. Now i want to remove the last element of them.

    I was thinking in storage every items in a QList<QGraphicsItem*>, get the last element and remove it with some function, but i am no be able to find that kind of function. Any suggerency?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Like ?

      QList<QGraphicsItem *> items = scene->Items();
      

      and then with
      http://doc.qt.io/qt-5/qlist.html#last
      and
      QGraphicsScene::removeItem(QGraphicsItem * item);
      you might need to delete it yourself with
      delete item;
      check docs.

      D 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Like ?

        QList<QGraphicsItem *> items = scene->Items();
        

        and then with
        http://doc.qt.io/qt-5/qlist.html#last
        and
        QGraphicsScene::removeItem(QGraphicsItem * item);
        you might need to delete it yourself with
        delete item;
        check docs.

        D Offline
        D Offline
        deleted396
        wrote on last edited by
        #3

        @mrjj sorry but i think that in that way is not possible because I am using QGraphicsTextItem, no QGraphicsItem

        mrjjM 1 Reply Last reply
        0
        • D deleted396

          @mrjj sorry but i think that in that way is not possible because I am using QGraphicsTextItem, no QGraphicsItem

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

          @AdrianCruz
          Hi
          The scene store items as base type (QGraphicsItem) .
          Even they are also subclasses types. Like QGraphicsTextItem.
          QGraphicsTextItem is also a QGraphicsItem.
          So you can just cast them using
          http://doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast
          and if cast is ok (return is not null) then , its that type. and you can delete it.

          1 Reply Last reply
          3
          • D Offline
            D Offline
            deleted396
            wrote on last edited by
            #5

            Finally, i did a cast how you said and now is working. Thanks so much!

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deleted396
              wrote on last edited by
              #6

              If someone has the same problem, i did:

              QGraphicsItem *item = qgraphicsitem_cast<QGraphicsItem*>(text);
              list.push_back(item);
              

              list is a QList<QGraphicsItem*> list.

              mrjjM 1 Reply Last reply
              0
              • D deleted396

                If someone has the same problem, i did:

                QGraphicsItem *item = qgraphicsitem_cast<QGraphicsItem*>(text);
                list.push_back(item);
                

                list is a QList<QGraphicsItem*> list.

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

                @AdrianCruz
                hi
                Super :)
                just as a note
                To avoid getting possible NULLs in the list, i would suggest doing

                QGraphicsItem *item = qgraphicsitem_cast<QGraphicsItem*>(text);
                if (item)
                list.push_back(item);
                
                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