I would personally write this:
for (int i = 0; i < layout->count(); ++i) {
QLayoutItem *layoutItem = layout->itemAt(i);
if (layoutItem->spacerItem()) {
layout->removeItem(layoutItem);
// You could also use: layout->takeAt(i);
delete layoutItem;
--i;
}
}
So, the logic would be this in a nutshell if the code does not make it clear:
Look up all the items of the layout.
Check if it is a spacer item.
If it is, remove it.