Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How pointers to widgets are handled?
Forum Updated to NodeBB v4.3 + New Features

How pointers to widgets are handled?

Scheduled Pinned Locked Moved Installation and Deployment
10 Posts 5 Posters 6.6k 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.
  • S Offline
    S Offline
    saeed144
    wrote on last edited by
    #1

    Dear all,
    In Qt doc examples, like tabdialog.pro example.
    There are some member (pointer) of Qt widgets like tabWidget
    defined in tabdialog.h

    @ private:
    QTabWidget *tabWidget;@

    used in tabdialog.cpp

    @tabWidget = new QTabWidget;
    @

    since no parent are set for these widgets, how/when are they deleted?
    Thanks,
    Saeed144

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      A parent is either set explicitly (parent passed to constructor, QObject::setParent()) or implicitly, for example when widgets are added to a layout or other widgets.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jim_kaiser
        wrote on last edited by
        #3

        Hi Saeed,

        Good observation there. But if you dig a bit further you will notice
        @
        mainLayout->addWidget(tabWidget);
        @

        When you look in the documentation of the QBoxLayout, it doesn't mention anything about ownership.. but if you dig further you will reach the addItem virtual function of QLayout.. where it is noted..

        bq. Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.

        and the addWidget in QLayout notes

        bq. Adds widget w to this layout in a manner specific to the layout. This function uses addItem().

        Ofcourse, the documentation doesn't specify explicitly that the QBoxLayout::addWidget uses the QLayout::addWidget.. but its a fairly valid assumption. The QBoxLayout implements addItem and should manage the memory mangement as defined by the QLayout interface. I can try to confirm this.. but maybe some trolls could intervene..

        1 Reply Last reply
        0
        • S Offline
          S Offline
          saeed144
          wrote on last edited by
          #4

          Thanks Lucas
          Thanks Jim, liked your elaboration on the subject.

          Replies are quite satisfactory,

          cheers
          Saeed

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deion
            wrote on last edited by
            #5

            So?!?! Who has the ownership to the widget?? It was not answered.

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

              The widget is owned by it's parent, whether the parent is set explicitly or by proxy of a layout.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deion
                wrote on last edited by
                #7

                Ok, I didn't actually have the time to extend QWidget and overwrite the destructor to put a qDebug() and see that it's being called, but I guess when using QHBoxLayout::addWidget() the layout takes the ownership of the added widget. The documentation should be more clear and specifically state this, without saying addWidget() uses addItem() and one has to go to addItem() and deduct that addWidget() takes ownership (because addItem() takes ownership).

                Thanks,
                Ionut

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

                  Well, if you think that, please go ahead and:

                  Report a bug against the documentation, or even better:

                  Update the documentation yourself, and submit it via Gerrit.

                  Qt is a community effort, and you're part of that community! You can make a difference. If you just post your griefs in a forum topic, chances are slim to none that they will be picked up and acted upon.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    [quote author="deion" date="1331894073"]Ok, I didn't actually have the time to extend QWidget and overwrite the destructor to put a qDebug() and see that it's being called, but I guess when using QHBoxLayout::addWidget() the layout takes the ownership of the added widget.[/quote]

                    Be aware that ownership and parentship are two different things.

                    If you add a widget to a layout, the layout becomes the owner of the widget, but it does not become the parent (only widgets can be a parent of a widget, and a layout is not a widget). The parent widget of the layout takes parentship, the layout takes ownership.

                    This also implies that widgets, which are added to a layout without a parent widget will leak memory as they are not deleted automatically due to having no parent.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      deion
                      wrote on last edited by
                      #10

                      I understand your point Andre, but there is one issue with this approach (maybe this is common to all community projects): I have to invest quite some time to follow the official upstream procedures; Complaining on forums actually DOES help :) I got to this thread by google-ing for "QBoxLayout addWidget ownership", so next time someone has the same question, there is a big chance he will end-up on this thread and see the discussion; so maybe it's not as helpful as formally reporting a bug/updating the documentation but it's still something.

                      PLUS: Complaining is FUN, following procedures is kind of boring :D

                      Regards

                      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