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. How does QSpacerItem get deleted?
QtWS25 Last Chance

How does QSpacerItem get deleted?

Scheduled Pinned Locked Moved Solved General and Desktop
spaceparentdestructor
8 Posts 3 Posters 6.0k 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.
  • A Offline
    A Offline
    alogim
    wrote on 16 Feb 2016, 10:36 last edited by alogim
    #1

    So, I know usually objects get deleted in Qt when their parents gets deleted. What about QSpacerItem, for example?
    Since it is not possible to set its parent, how does it get deleted if I write

        spacerItem = new QSpacerItem(10, 10);
    

    in a .cpp file, where spacerItem is declared in a .h file? Do I have to take care of deletions by myself?

    K 1 Reply Last reply 16 Feb 2016, 10:41
    0
    • A alogim
      16 Feb 2016, 10:36

      So, I know usually objects get deleted in Qt when their parents gets deleted. What about QSpacerItem, for example?
      Since it is not possible to set its parent, how does it get deleted if I write

          spacerItem = new QSpacerItem(10, 10);
      

      in a .cpp file, where spacerItem is declared in a .h file? Do I have to take care of deletions by myself?

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 16 Feb 2016, 10:41 last edited by kshegunov
      #2

      @alogim
      Hello,
      The layout owns the QLayoutItem instances and will delete them when it goes out of scope. If you don't add the spacer to a layout, then it's your responsibility to delete the object.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        alogim
        wrote on 16 Feb 2016, 10:42 last edited by
        #3

        @kshegunov Thank you very much, I missed that on the docs :)
        Now all makes sense, thank you again.

        K 1 Reply Last reply 16 Feb 2016, 10:45
        0
        • A alogim
          16 Feb 2016, 10:42

          @kshegunov Thank you very much, I missed that on the docs :)
          Now all makes sense, thank you again.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 16 Feb 2016, 10:45 last edited by kshegunov
          #4

          @alogim
          As an additional note:
          If you wish to delete an item that's been held by a layout, you should detach it first by using QLayout::takeAt. Then the ownership of the item will no longer belong to the layout, so you can safely delete it. You should take care not to delete the item if it's handled by a layout.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 16 Feb 2016, 10:59 last edited by SGaist
            #5

            Hi,

            To add to @kshegunov, if you don't use spacerItem in any layout (very unlikely but still) then it's your job to delete it.

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

            K 1 Reply Last reply 17 Feb 2016, 06:30
            0
            • S SGaist
              16 Feb 2016, 10:59

              Hi,

              To add to @kshegunov, if you don't use spacerItem in any layout (very unlikely but still) then it's your job to delete it.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 17 Feb 2016, 06:30 last edited by
              #6

              @SGaist
              This actually was my first post's point ;)

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 17 Feb 2016, 08:00 last edited by
                #7

                @kshegunov Err… I somehow managed to misread that last statement :D

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

                K 1 Reply Last reply 17 Feb 2016, 08:03
                0
                • S SGaist
                  17 Feb 2016, 08:00

                  @kshegunov Err… I somehow managed to misread that last statement :D

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 17 Feb 2016, 08:03 last edited by
                  #8

                  @SGaist
                  It happens even to the best ... at the very least we both support it, so that has to account for something ;)

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0

                  7/8

                  17 Feb 2016, 08:00

                  • Login

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