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]Little question about memory when delete

[SOLVED]Little question about memory when delete

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 3.9k 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.
  • E Offline
    E Offline
    evergreen
    wrote on 30 Jun 2011, 10:10 last edited by
    #1

    Hello,

    I'm currently using a window which has a layout principal. It's contains loads of object such as QPushButtons, QLabel...

    In my destructor, I destroy the layout and all the widgets.
    Ad the layout countains all the widget, I was wondering if doing just "delete layout" would be enough to get a clean memory.

    Thanks

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eddy
      wrote on 30 Jun 2011, 10:23 last edited by
      #2

      the layout and widgets are all childs of the window. When the parent is destroyed so will the children.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • E Offline
        E Offline
        evergreen
        wrote on 30 Jun 2011, 10:27 last edited by
        #3

        And the widget included in a layout are also considered as children of the layout?

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Eddy
          wrote on 30 Jun 2011, 10:35 last edited by
          #4

          you only have to destroy the window. The layout and all it's children will be destroyed automatically.

          I you're using code not Qt designer, make sure you use the parent parameter where needed.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on 30 Jun 2011, 10:38 last edited by
            #5

            Hi,

            one clearification:
            The layout has no widgets as children. The layout is some organizational stuff, that is attached as child to a widget. The widgets inside a layout are chidlren of the parent of the layout.

            Deleting the main window containing the top layout is enough to delete all children (widgets, layouts and other QObject derived classes)

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thisisbhaskar
              wrote on 30 Jun 2011, 11:18 last edited by
              #6

              http://doc.qt.nokia.com/latest/layout.html

              Look at "Tips for Using Layouts" section.

              EDIT:
              Tips for Using Layouts

              When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

              Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

              You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on 30 Jun 2011, 11:55 last edited by
                #7

                Hi Vijay Bhaska Reddy, The question was about memory, and when it is deleted, it was not related to layouts in general.

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  evergreen
                  wrote on 30 Jun 2011, 12:47 last edited by
                  #8

                  Okay,

                  It's not so simple tho, I really have to check each widget if they are only parented to the layout and not another widget, if I followed what you all said.

                  Thanks for all the answers.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on 30 Jun 2011, 12:56 last edited by
                    #9

                    They are reparented by default. If you add hem to the layout, the layout will reparent them to it#s widget. It works.

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      ludde
                      wrote on 30 Jun 2011, 12:58 last edited by
                      #10

                      It's actually really simple. If you've added a widget X to a layout or widget that is part of another widget Y, X will be deleted when Y is deleted. This works recursively, so you really just have to delete the top-level widget.

                      1 Reply Last reply
                      0

                      4/10

                      30 Jun 2011, 10:35

                      • Login

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