5

I have a very basic doubt regarding QGridLayout. For adding a widget in QGridLayout we give QWidget * that should be added along with the row & column no (some other args as well). Now for removing a widget there is no function to remove a widget according to row & column no i.e. something like this:

int row, column;
gridObj->remove(row, column);

I think QGridLayout must be maintaining a sort of QList to store references of Widgets & there positions. So why is there no function for removing widgets by position only? It only has 1 remove function for which we need to specify the reference of QWidget object.

If this is a limitation somehow then is there a workaround for this problem? Maintaining a QList by myself is a solution but it is pretty tedious. Thank You

Cool_Coder
  • 4,888
  • 16
  • 57
  • 99

2 Answers2

5

I might be mistaken here, but from skimming the documentation, try this:

  1. Get the QLayoutItem at the position (QGridLayout::itemAtPosition(row, column)).
  2. Use the QLayoutItem to get the widget pointer (QLayoutItem::widget()).
  3. Use the widget pointer to find the index of the widget in the QGridLayout (QLayout::indexOf(widgetPointer)).
  4. Use the index to take ownership of the widget from the layout (QGridLayout::takeAt(index)).
  5. Wrap it all in a convenience function?

I've always had trouble reordering widgets in layouts, removing widgets from layouts, and etc... Oftentimes, I just resort to deleting the layout and re-adding the widgets. =(

Jamin Grey
  • 10,151
  • 6
  • 39
  • 52
  • Thanks a lot for that answer, I will try that & get back to you. Regarding the complexities of layouts in Qt, I also feel the same. Sometimes I wonder why did they make this so ridiculously confusing to use. But anyway rest of the Qt is pretty fine to understand... – Cool_Coder Mar 24 '13 at 13:03
  • In a grid layout, what is the no returned by QLayout::indexOf()? Is it the row or column no, or is it simply some index of a list? – Cool_Coder Mar 24 '13 at 13:08
  • I'm fairly sure it's a 1-dimension index into a list. – Jamin Grey Mar 24 '13 at 16:26
4

For removing a widget within a QGridLayout by layout position, you can simply use

layout->removeWidget(layout->itemAtPosition(row, column)->widget());

However, you have to note the following about it:

  • This code assumes that there actually is an item on the specified position in the layout. If it isn't, itemAtPosition() will return null. So you need to be sure about the position, or check it explicitly.
  • This code will remove the widget from the layout, but not delete it. You have to put the widget back into a different layout or give the widget a reasonable geometry yourself. If you don't, the widget will simply keep visible at its current position. If you want the widget to be destructed, you'll have to delete it explicitly after it has been removed from the layout.
  • This code will only work for top-level widgets within the layout which have been added with addWidget(). It won't work for nested layouts added with addLayout(). If you need to care about nested layouts as well, see my answer about removing rows and columns from grid layouts.
Community
  • 1
  • 1
emkey08
  • 5,059
  • 3
  • 33
  • 34