Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved Remove QGraphicsTextItem from QGraphicsScene

    General and Desktop
    qgraphicsscene remove textitem qgraphicstextit
    2
    7
    1643
    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.
    • D
      deleted396 last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by 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 1 Reply Last reply Reply Quote 3
        • D
          deleted396 @mrjj last edited by

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

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @deleted396 last edited by mrjj

            @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 Reply Quote 3
            • D
              deleted396 last edited by

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

              1 Reply Last reply Reply Quote 0
              • D
                deleted396 last edited by

                If someone has the same problem, i did:

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

                list is a QList<QGraphicsItem*> list.

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @deleted396 last edited by

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