Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidgetItem setParent
Qt 6.12 LTS is out! See what's new in the release blog

QTreeWidgetItem setParent

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 3.7k 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
    wrote on last edited by
    #2

    use addChild method of QTreeWidgetItem.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    https://www.pthinks.com

    1 Reply Last reply
    5
    • QT-static-prgmQ Offline
      QT-static-prgmQ Offline
      QT-static-prgm
      wrote on last edited by
      #3

      Let's say I add a few Childs and subchilds and later I delete the tree widget. Will that automatically delete all entries in the tree widget??

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Matt86
        wrote on last edited by
        #4

        In general, any time you delete a UI object any of the children will be deleted too.

        1 Reply Last reply
        0
        • QT-static-prgmQ QT-static-prgm

          Hi,

          I just wanted to set the parent of an QTreeWidget AFTER it is created. But for some reason the function is not available. Any ideas why?? I can set the parent in the constructor and i can read that parent with .parent() but i cannot set it.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @QT-static-prgm Why do you think there is no setParent() method? QTreeWidget has setParent() method (inherited from QObject). QObject has setParent method, that means ALL classes derived from QObject have it.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • QT-static-prgmQ Offline
            QT-static-prgmQ Offline
            QT-static-prgm
            wrote on last edited by
            #6

            but the qtreewidgetItem has no such function. My compiler gives an error

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
              wrote on last edited by
              #7

              QTreeWidgetItem does not have setParent. QTreeWidget has. So you should use addChild only. QTreeWidgetItem is not inherited from QObject. QTreeWidget is inherited from QObject. So QTreeWidget has setParent.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              https://www.pthinks.com

              1 Reply Last reply
              4
              • QT-static-prgmQ Offline
                QT-static-prgmQ Offline
                QT-static-prgm
                wrote on last edited by
                #8

                All right thank you :D

                No i have an other problem.

                When i use .clear() to remove everything and then refill the tree it seams that the actual items are still alive. Is there a better way to delete them??

                (i noticed that when i refreshed 3 times the connect signal is triggered 3 times)

                J.HilkJ 1 Reply Last reply
                0
                • QT-static-prgmQ QT-static-prgm

                  All right thank you :D

                  No i have an other problem.

                  When i use .clear() to remove everything and then refill the tree it seams that the actual items are still alive. Is there a better way to delete them??

                  (i noticed that when i refreshed 3 times the connect signal is triggered 3 times)

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @QT-static-prgm

                  Really? Are you using normal SIGNAL/SLots or Lambda functions?

                  Lambdas don't get automatically removed.

                  Anyway you can try something like this, to delete the children.

                  for(auto a : treewidget->children()
                      delete a;
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • QT-static-prgmQ Offline
                    QT-static-prgmQ Offline
                    QT-static-prgm
                    wrote on last edited by
                    #10

                    i use lambda.

                    J.HilkJ 1 Reply Last reply
                    0
                    • QT-static-prgmQ QT-static-prgm

                      i use lambda.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @QT-static-prgm

                      there's your problem then. Either use 'normal' functions, or manually disconnect the connection.
                      How to do that is taken from here

                      New way using QMetaObject::Connection
                      QMetaObject::Connection m_connection;
                      //…
                      m_connection = QObject::connect(…);
                      //…
                      QObject::disconnect(m_connection);
                      Works in all cases, including lambda functions or functors.
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1
                      • QT-static-prgmQ Offline
                        QT-static-prgmQ Offline
                        QT-static-prgm
                        wrote on last edited by
                        #12

                        i changed to singal/slot instead of lambda and had the same problem. Only after i disconnected manual using disconnect(ptr, signal, ptr, slot) it worked.

                        So it's fine for me that it works, but does that mean that there is still garbage in the memory or is it now correctly deleted??

                        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
                        • Unsolved