Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED] QObject::setParent() vs passing this as parent

[SOLVED] QObject::setParent() vs passing this as parent

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 4 Posters 14.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.
  • T Offline
    T Offline
    terietor
    wrote on 17 Aug 2011, 18:36 last edited by
    #1

    Hello,

    I have two similar code snippets.The first one(Qobject::setParent) is working correct and the second one(this keyword) doesn't.

    @IamAQDialog::IamAQDialog()
    {
    QHBoxLayout *hoz = new QHBoxLayout();
    hoz->setParent(this);

    m_loadButton->setDisabled(true);
    hoz->addWidget(m_loadButton);
    
    m_removeMenuButton->setDisabled(true);
    hoz->addWidget(m_removeMenuButton);
    
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(m_projectList);
    lay->addLayout(hoz);
    setLayout(lay);
    

    }@

    output: !http://imageshack.us/photo/my-images/827/setparent.png/()!

    @IamAQDialog::IamAQDialog()
    {
    QHBoxLayout *hoz = new QHBoxLayout(this);

    m_loadButton->setDisabled(true);
    hoz->addWidget(m_loadButton);
    
    m_removeMenuButton->setDisabled(true);
    hoz->addWidget(m_removeMenuButton);
    
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->addWidget(m_projectList);
    lay->addLayout(hoz);
    setLayout(lay);
    

    }@

    output: !http://imageshack.us/photo/my-images/843/thisparent.png/()!

    I am trying to parent the hoz widget in order to avoid mem leak.

    thanks in advance

    terietor.gr

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 17 Aug 2011, 18:42 last edited by
      #2

      Just a remark to your code formatting here in the first section:

      [quote author="Giorgos Tsiapaliwkas" date="1313606213"]
      @IamAQDialog::IamAQDialog()
      {
      QHBoxLayout *hoz = new QHBoxLayout();
      hoz->setParent(this);

      m_loadButton->setDisabled(true);
      

      @
      [/quote]

      Apparently you should not use the devnet formatting (at least the underlines) in code sections.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • T Offline
        T Offline
        terietor
        wrote on 17 Aug 2011, 18:54 last edited by
        #3

        [quote author="koahnig" date="1313606536"]=
        Apparently you should not use the devnet formatting (at least the underlines) in code sections. [/quote]

        yes,you have right.
        thanks

        terietor.gr

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on 17 Aug 2011, 18:59 last edited by
          #4

          There is no need to explicitly set a parent for hoz. addLayout() and setLayout() automatically reparents the layout.

          EDIT When passing the parent as ctor parameter the parent's setLayout() method is called, whereas when setting the parent using setParent() it isn't. In your second listing the hoz layout isn't added to the lay layout because it is already set as the layout for this (assumption).

          Conclusion. Both listings are "wrong" in their way. Just omit the explicit parenting.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            terietor
            wrote on 17 Aug 2011, 19:42 last edited by
            #5

            [quote author="Lukas Geyer" date="1313607566"] Just omit the explicit parenting.[/quote]

            The memory deallocation will happen automatically i guess.Correct?

            terietor.gr

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on 17 Aug 2011, 20:04 last edited by
              #6

              For layouts, if they are correctly added, yes.
              What also works is just setting the parent for the top layout (never for child layouts, added by addLayout).

              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
                terietor
                wrote on 17 Aug 2011, 20:20 last edited by
                #7

                thanks i will mark it as solved

                terietor.gr

                1 Reply Last reply
                0

                1/7

                17 Aug 2011, 18:36

                • Login

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