Remove Widget from QGridLayout in Qt?
c++, qt
Solution
I might be mistaken here, but from skimming the documentation, try this:
- Get the QLayoutItem at the position (QGridLayout::itemAtPosition(row, column)).
- Use the QLayoutItem to get the widget pointer (QLayoutItem::widget()).
- Use the widget pointer to find the index of the widget in the QGridLayout (QLayout::indexOf(widgetPointer)).
- Use the index to take ownership of the widget from the layout (QGridLayout::takeAt(index)).
- 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. =(
Problem
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