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 27 Jun 2018, 17:19 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Jun 2018, 17:46 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 27 Jun 2018, 17:55
      3
      • M mrjj
        27 Jun 2018, 17:46

        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 27 Jun 2018, 17:55 last edited by
        #3

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

        M 1 Reply Last reply 27 Jun 2018, 18:11
        0
        • D deleted396
          27 Jun 2018, 17:55

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Jun 2018, 18:11 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 28 Jun 2018, 10:51 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 28 Jun 2018, 12:27 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.

              M 1 Reply Last reply 29 Jun 2018, 07:27
              0
              • D deleted396
                28 Jun 2018, 12:27

                If someone has the same problem, i did:

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

                list is a QList<QGraphicsItem*> list.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 29 Jun 2018, 07:27 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

                6/7

                28 Jun 2018, 12:27

                • Login

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