14

I try to remove widgets from a specified row in a QGridLayout like this:

void delete_grid_row(QGridLayout *layout, int row)
{
    if (!layout || row < 0) return;

    for (int i = 0; i < layout->columnCount(); ++i) {
        QLayoutItem* item = layout->itemAtPosition(row, i);
        if (!item) continue;

        if (item->widget()) {
            layout->removeWidget(item->widget());
        } else {
            layout->removeItem(item);
        }
        delete item;
    }
}

But when I call it, the app crashes with SIGSEGV on delete item in the first iteration. Any ideas?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
maverik
  • 5,508
  • 3
  • 35
  • 55

2 Answers2

31

Short answer: Use the code provided below

Removing a row or column (or even a single cell) from a QGridLayout is tricky. Use the code provided below.

Long answer: Digging into QGridLayout details

First, note that QGridLayout::rowCount() and QGridLayout::columnCount() always return the number of internally allocated rows and columns in the grid layout. As an example, if you call QGridLayout::addWidget(widget,5,7) 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.

Note that it's unfortunately impossible 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.

What you can do is to remove the contents 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 remain unchanged.

So how can the contents of a row or column (or cell) be cleared? This unfortunately also isn't as easy as it might seem.

First, you need to think about if you only want to remove the widgets from the layout, or if you also want them to become deleted. 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.

Next, you have to consider that a layout cell can not just only contain a widget, but also a nested layout, which itself can contain nested layouts, and so on. You further need to handle layout items which span over multiple rows and columns. And, finally, there are some row and column attributes like minimum widths and heights which don't depend on the actual contents but still have to be taken care of.

The code

#include <QGridLayout>
#include <QWidget>

/**
 * Utility class to remove the contents of a QGridLayout row, column or
 * cell. If the deleteWidgets parameter is true, then the widgets become
 * not only removed from the layout, but also deleted. Note that we won't
 * actually remove any row or column itself from the layout, as this isn't
 * possible. So the rowCount() and columnCount() will always stay the same,
 * but the contents of the row, column or cell will be removed.
 */
class GridLayoutUtil {

public:

  // Removes the contents of the given layout row.
  static void removeRow(QGridLayout *layout, int row, bool deleteWidgets = true) {
    remove(layout, row, -1, deleteWidgets);
    layout->setRowMinimumHeight(row, 0);
    layout->setRowStretch(row, 0);
  }

  // Removes the contents of the given layout column.
  static void removeColumn(QGridLayout *layout, int column, bool deleteWidgets = true) {
    remove(layout, -1, column, deleteWidgets);
    layout->setColumnMinimumWidth(column, 0);
    layout->setColumnStretch(column, 0);
  }

  // Removes the contents of the given layout cell.
  static void removeCell(QGridLayout *layout, int row, int column, bool deleteWidgets = true) {
    remove(layout, row, column, deleteWidgets);
  }

private:

  // Removes all layout items which span the given row and column.
  static void remove(QGridLayout *layout, int row, int column, bool deleteWidgets) {
    // We avoid usage of QGridLayout::itemAtPosition() here to improve performance.
    for (int i = layout->count() - 1; i >= 0; i--) {
      int r, c, rs, cs;
      layout->getItemPosition(i, &r, &c, &rs, &cs);
      if (
          (row == -1 || (r <= row && r + rs > row)) &&
          (column == -1 || (c <= column && c + cs > column))) {
        // This layout item is subject to deletion.
        QLayoutItem *item = layout->takeAt(i);
        if (deleteWidgets) {
          deleteChildWidgets(item);
        }
        delete item;
      }
    }
  }

  // Deletes all child widgets of the given layout item.
  static void deleteChildWidgets(QLayoutItem *item) {
    QLayout *layout = item->layout();
    if (layout) {
      // Process all child items recursively.
      int itemCount = layout->count();
      for (int i = 0; i < itemCount; i++) {
        deleteChildWidgets(layout->itemAt(i));
      }
    }
    delete item->widget();
  }
};
emkey08
  • 5,059
  • 3
  • 33
  • 34
  • I think this line should use && not ||. `if ((r <= row && r + rs - 1 >= row) && (c <= column && c + cs - 1 >= column))`. Otherwise every cells in the same row or column will be deleted. – Nuntipat Narkthong Jan 07 '14 at 06:11
  • The remove function is just a helper for removeRow and removeColumn here, so it is correct. But yes, if you just want to remove a single cell from the layout, then you can adapt the remove function from above like you said. – emkey08 Jan 14 '14 at 10:50
3

The QGridLayout itself is managing the QLayoutItem's. I believe the moment you call removeWidget the item will be deleted. Thus you have an invalid pointer at that point. Attempting to do anything with it, not just delete, will fail.

Thus, just don't delete it, you'll be fine.

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
  • 2
    Hm... actually docs say `Removes the layout item item from the layout. It is the caller's responsibility to delete the item.` – maverik Mar 22 '11 at 17:35
  • I was a bit quick with my answer. `removeWidget` has no such note, and presumable deletes the associated QLayoutItem (as presumably you may not have a pointer to it). So either remove only by widget or put the delete inside the `else` clause. – edA-qa mort-ora-y Mar 22 '11 at 17:48
  • If I put delete inside the `else` clause the app doesn't crash but widgets are still visible (although they deleted from the grid) – maverik Mar 22 '11 at 17:52
  • Yes, you've simply removed the item. The docs do state the widget is still there: you need to delete the widget. The API here isn't so clear. I wonder if you could just do `delete item` and skip the remove statements altogether (that works in most other cases in Qt). – edA-qa mort-ora-y Mar 23 '11 at 08:30
  • 4
    I looked at the sources of qt 4.7.2. `removeWidget` calls `delete takeAt(...)` where `removeItem` just calls `takeAt(...)`. I mark your answer as an accepted. Thanks. – maverik Mar 24 '11 at 08:08
  • Ah yes, access to the source is great in face of slightly confusing docs. :) – edA-qa mort-ora-y Mar 24 '11 at 09:05