I have a QGridLayout with 3 columns, created like this
self.my_grid = QGridLayout()
self.my_grid.setColumnStretch(2, 0)
After filling the grid with 10 items, it looks as expected like this:
X X X
X X X
X X X
X
At certain events the grid items change and I try to erase all items and add the new ones:
def updateGrid(self, taskList):
# delete existing items from grid
for i in reversed(range(self.my_grid.count())):
widgetToRemove = self.my_grid.itemAt(i).widget()
widgetToRemove.setParent(None)
widgetToRemove.deleteLater()
# add new buttons
for task in tasklist:
btn = QPushButton(task)
self.my_grid.addWidget(btn)
But then it shows with an empty cell like this:
X X
X X X
X X X
X X
And after calling updateGrid again, it has two empty cells like this:
X
X X X
X X X
X X X
And finally, after forcing one more update again I get what I expected:
X X X
X X X
X X X
X
How can I avoid the empty cells?