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 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?

    kshegunovK 1 Reply Last reply
    0
    • A alogim

      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?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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 last edited by
        #3

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

        kshegunovK 1 Reply Last reply
        0
        • A alogim

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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

            kshegunovK 1 Reply Last reply
            0
            • SGaistS SGaist

              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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on 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
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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

                kshegunovK 1 Reply Last reply
                0
                • SGaistS SGaist

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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

                  • Login

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