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. [SOLVED] Remove an item from the child items list of QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Remove an item from the child items list of QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.5k Views 1 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.
  • I Offline
    I Offline
    isaacEnrique
    wrote on last edited by
    #1

    Greetings.

    How I can remove an item from the child items list of QGraphicsItem?.

    So far I have considered using the following:

    @
    QGraphicsItem::childItems()
    QGraphicsItem::setParentItem(QGraphicsItem*)
    QList::removeAt(int i) /* or a similar function for remove a item of a list */
    @

    I do not know in what order to invoke the last two functions.
    Nor if there is a cleaner, efficient and safe way to do what I want so.

    Thanks in advance for any help and/or suggestions.

    Isaac Pérez
    Programming is understanding.

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

      Hi,

      AFAIK, setting it's parent to something else should remove it from the list so your wouldn't need that last line.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isaacEnrique
        wrote on last edited by
        #3

        After establishing the new parent of the child item, eg using

        @myItem->setParentItem(0)@

        ... I can free the memory of recently removed child item?

        Isaac Pérez
        Programming is understanding.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          If you want to delete child item you do not need to remove it,
          just delete it.

          delete item;

          1 Reply Last reply
          0
          • I Offline
            I Offline
            isaacEnrique
            wrote on last edited by
            #5

            When I say "remove" I mean establishing a new parent item for the child item; with @QGraphicsItem::setParentItem()@

            Now, when you say:

            bq. If you want to delete child item you do not need to remove it,
            just delete it.

            You mean that's not necessary and that I can use the operator 'delete' on chil item (without invoking 'QGraphicsItem::setParentItem()' before)?

            Isaac Pérez
            Programming is understanding.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by
              #6

              yes, deleting item will delete all children and remove it from the scene

              1 Reply Last reply
              0
              • I Offline
                I Offline
                isaacEnrique
                wrote on last edited by
                #7

                bq. yes, deleting item will delete all children and remove it from the scene

                My uncertainty is about the status of the parent item of the item removed.

                Will doing 'delete' an item from the list of child items, in the way you suggest me, not give rise to any inconsistency?

                Isaac Pérez
                Programming is understanding.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #8

                  The deleted GraphicsItem takes care of that in its destructor.

                  Edit: However, should you have a copy of the childItems list, then you can potentially use an invalid pointer. Naturally.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    isaacEnrique
                    wrote on last edited by
                    #9

                    Well, all doubts were clarified.

                    Thanks you all!

                    Isaac Pérez
                    Programming is understanding.

                    1 Reply Last reply
                    0

                    • Login

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