Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Short answer</h2> <p>Removing a row or column from a <code>QGridLayout</code> is tricky. Use the <code>removeRow()</code> and <code>removeColumn()</code> functions from the code provided below.</p> <pre><code>/** * Removes all layout items on the given @a row from the given grid * @a layout. If @a deleteWidgets is true, all concerned child widgets * become not only removed from the layout, but also deleted. Note that * this function doesn't actually remove the row itself from the grid * layout, as this isn't possible (i.e. the rowCount() and row indices * will stay the same after this function has been called). */ void removeRow(QGridLayout *layout, int row, bool deleteWidgets) { remove(layout, row, -1, deleteWidgets); layout-&gt;setRowMinimumHeight(row, 0); layout-&gt;setRowStretch(row, 0); } /** * Removes all layout items on the given @a column from the given grid * @a layout. If @a deleteWidgets is true, all concerned child widgets * become not only removed from the layout, but also deleted. Note that * this function doesn't actually remove the column itself from the grid * layout, as this isn't possible (i.e. the columnCount() and column * indices will stay the same after this function has been called). */ void removeColumn(QGridLayout *layout, int column, bool deleteWidgets) { remove(layout, -1, column, deleteWidgets); layout-&gt;setColumnMinimumWidth(column, 0); layout-&gt;setColumnStretch(column, 0); } /** * Helper function. Removes all layout items within the given @a layout * which either span the given @a row or @a column. If @a deleteWidgets * is true, all concerned child widgets become not only removed from the * layout, but also deleted. */ void remove(QGridLayout *layout, int row, int column, bool deleteWidgets) { // We avoid usage of QGridLayout::itemAtPosition() here to improve performance. for (int i = layout-&gt;count() - 1; i &gt;= 0; i--) { int r, c, rs, cs; layout-&gt;getItemPosition(i, &amp;r, &amp;c, &amp;rs, &amp;cs); if ((r &lt;= row &amp;&amp; r + rs - 1 &gt;= row) || (c &lt;= column &amp;&amp; c + cs - 1 &gt;= column)) { // This layout item is subject to deletion. QLayoutItem *item = layout-&gt;takeAt(i); if (deleteWidgets) { deleteChildWidgets(item); } delete item; } } } /** * Helper function. Deletes all child widgets of the given layout @a item. */ void deleteChildWidgets(QLayoutItem *item) { if (item-&gt;layout()) { // Process all child items recursively. for (int i = 0; i &lt; item-&gt;layout()-&gt;count(); i++) { deleteChildWidgets(item-&gt;layout()-&gt;itemAt(i)); } } delete item-&gt;widget(); } </code></pre> <h2>Long answer: the details about this</h2> <p>First, note that <code>QGridLayout::rowCount()</code> and <code>QGridLayout::columnCount()</code> always return the number of <strong>internally allocated</strong> rows and columns in the grid layout. As an example, if you call <code>QGridLayout::addWidget(widget,5,7)</code> on a freshly constructed grid layout, the row count will be 6 and the column count will be 8, and all cells of the grid layout except the cell on index (5,7) will be empty and thus invisible within the GUI.</p> <p>Note that it's unfortunately <strong>impossible</strong> to remove such an internal row or column from the grid layout. In other words, the row and column count of a grid layout can always only grow, but never shrink.</p> <p>What you <em>can</em> do is to remove the <strong>cell contents</strong> of a row or column, which will effectively have the same visual effect as removing the row or column itself. But this of course means that all row and column counts and indices will <strong>remain unchanged</strong>.</p> <p>So how can the contents of a row or column be cleared? This unfortunately also isn't as easy as it might seem.</p> <p>First, you need to think about if you only want to remove the widgets <strong>from the layout</strong>, or if you also want them to <strong>become deleted</strong>. If you only remove the widgets from the layout, you must put them back into a different layout afterwards or manually give them a reasonable geometry. If the widgets also become deleted, they will disappear from the GUI. The provided code uses a boolean parameter to control widget deletion.</p> <p>Next, you have to consider that a layout cell can not just only contain a widget, but also a <strong>nested layout</strong>, which itself can contain nested layouts, and so on. You further need to handle layout items which span over <strong>multiple rows and columns</strong>. And, finally, there are some row and column <strong>attributes</strong> like minimum widths and heights which don't depend on the actual cell contents but still have to be taken care of.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload