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. QLayout attempting to add qlayout
Forum Updated to NodeBB v4.3 + New Features

QLayout attempting to add qlayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.3k 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.
  • G Offline
    G Offline
    gully
    wrote on last edited by gully
    #1

    My hierarchy is the following.
    A Dialog contains Vertical Layout
    Vertical Layout contains a Tab Widget and a Horizontal Layout
    The Horizontal Layout contains a label and a textbox.

    My goal is to ensure that right parenting is done to the controls and the layouts so that I donot have to clean-up objects/garbage collect the controls created with

    new
    
    The code is of the following
        dialog = new QDialog();
        dialog_vertical_layout = new QVBoxLayout(dialog);
        tabWidget = new QTabWidget(dialog);
    
        executeMetadataLayout=new QHBoxLayout(dialog);
        execute_button=new QPushButton(dialog);
        execute_button->setText(QString("Execute"));
    
        dialog_vertical_layout->addWidget(tabWidget);
        dialog_vertical_layout->addLayout(executeMetadataLayout);
    
        dialog->setAttribute(Qt::WA_DeleteOnClose);
        dialog->show();
    

    I am getting warnings with QLayout attempting to add qlayout what is going wrong?
    Also to achieve my goals am I in the right direction?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gully said in QLayout attempting to add qlayout:

      executeMetadataLayout=new QHBoxLayout(dialog);

      You're creating a second layout with dialog as parent. Since this ctor automatically adds the layout to the widget you'll get an error because there is already a layout set.
      I don't see a reason for the second layout since you've only two widgets which perfectly fit into the first QVBoxLayout.
      Also I would use Qt designer instead creating the ui elements by hand.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gully
        wrote on last edited by
        #3

        @Christian-Ehrlicher I am setting the parent so that I donot have to do garbage collection separately. The parent child relationship ensures that the child objects (created with new especially) are deleted once the parent is destroyed.

        How can I in this case maintain automatic garbage collection and set the layouts at the same time?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can only set one layout to one widget at the same time (therefore the warning). As I said - I don't see what's the second layout is for in your example. Simply use the Qt designer for such stuff and you're fine.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gully
            wrote on last edited by
            #5

            @Christian-Ehrlicher using QT designer is just going to help me run away from this problem rather than solving it. I have my reasons not to use QT Designer.

            I understand where I am going wrong. I know how to solve it as well. What I donot understand is how can I create layout as well as maintain the parent-child relationship so that automatic garbage collection is ensured

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Since a widget can't have two layouts I don't see a reason why you need a second layout with this widget as parent. Just use the widget where the layout is used for (which is done implicit in the ctor or with widget->setLayout())

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gully
                wrote on last edited by
                #7

                @Christian-Ehrlicher just looking for your confirmation/rejection here , if I do setWidget() or setLayout does these function automatically set parent child relationship- so that the objects created on heap are not required to be garbage collection manually/writing code for it ?

                aha_1980A 1 Reply Last reply
                0
                • G gully

                  @Christian-Ehrlicher just looking for your confirmation/rejection here , if I do setWidget() or setLayout does these function automatically set parent child relationship- so that the objects created on heap are not required to be garbage collection manually/writing code for it ?

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @gully just use the help: http://doc.qt.io/qt-5/qlayout.html#addItem which is called from addWidget, takes ownership of the added widget. Thats what you want.

                  Qt has to stay free or it will die.

                  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