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. Can't remove HLayout from VLayout
Forum Updated to NodeBB v4.3 + New Features

Can't remove HLayout from VLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.1k Views 2 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.
  • M Offline
    M Offline
    MoschopsRedux
    wrote on last edited by
    #1

    I have a UI. That UI contains a vertical layout.

    I add multiple horizontal layouts to that layout; each horizontal layout contains some widgets. In effect, when i add horizontal layouts, I'm adding rows of buttons. Here is how I am adding these rows of buttons:

    QHBoxLayout *hLayout = new QHBoxLayout;
    
    QPushButton* remButton = PBS_NEW QPushButton();
    hLayout->addWidget(remButton);
    QComboBox* comboBox1 = PBS_NEW QComboBox();
    hLayout->addWidget(comboBox1);
    QComboBox* comboBox2 = PBS_NEW QComboBox();
    hLayout->addWidget(comboBox2);
    QLineEdit* lineEdit = PBS_NEW QLineEdit();
    hLayout->addWidget(lineEdit);
    QCheckBox* box = PBS_NEW QCheckBox();
    hLayout->addWidget(box);
    
    ui.matchCriteriaVLayout->addLayout(hLayout);
    

    At a later time, I want to remove that row of buttons. I decide to do this by removing the horizontal layout. But it doesn't work. The buttons are still visible (and pressable) although the formatting seems to go wrong; the row of buttons overlaps other rows.

    Here is how I am trying to remove that row of buttons:

    // take the horizonal layout that needs to be removed
    QHBoxLayout* layoutToRemove = static_cast<QHBoxLayout*>(ui.matchCriteriaVLayout->takeAt(rowToRemove));
    
    // delete all horiztonal layout's children (i.e. contents)
    QLayoutItem *child;
    while ((child = layoutToRemove->takeAt(0)) != 0)
    {
    	delete child;
    }
    
    // delete the hlayout itself
    delete layoutToRemove;
    

    What am I doing wrong? What am I missing?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Can u try inside your while loop
      delete child->widget()
      delete child

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      9
      • M Offline
        M Offline
        MoschopsRedux
        wrote on last edited by MoschopsRedux
        #3

        Brilliant. That certainly makes it look a lot better. I will double-check that it is deleteing everything and I'm not leaving the layout in a bad state, but so far that looks good.

        Thank you very much. If this works out well, I'll come back and mark this solved.

        A 1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Cool. If the issue is resolved, request you to move the issue to SOLVED state.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1
          • M MoschopsRedux

            Brilliant. That certainly makes it look a lot better. I will double-check that it is deleteing everything and I'm not leaving the layout in a bad state, but so far that looks good.

            Thank you very much. If this works out well, I'll come back and mark this solved.

            A Offline
            A Offline
            ambershark
            wrote on last edited by ambershark
            #5

            @MoschopsRedux The only problem with this solution is your layout, potentially, does not own those widgets. If you do the delete child->widget() you are deleting something another component may use later on and your app will crash.

            This may not be the actual case in your specific application but it's a bad habit to get into.

            You would be better off doing a child->widget()->deleteLater() to let it handle any outstanding messages and let Qt's message loop clean it up. This will cause it's parents to forget about it properly and not think the memory for their children still exists.

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            2

            • Login

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