Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Widget from QGridLayout in Qt? [duplicate]

Tags:

c++

qt

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

like image 375
Cool_Coder Avatar asked Mar 23 '13 16:03

Cool_Coder


1 Answers

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. =(

like image 189
Jamin Grey Avatar answered Sep 20 '22 14:09

Jamin Grey