Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete all QSpacerItem from a layout

I've added a QSpacerItem to a layout using its addStretch() method.

layout->addStretch(1);

now i'd like to delete it but i didn't have any reference to it.

how can I browse all QLayoutItem and only delete QSpacerItem ?

like image 293
Ghilas BELHADJ Avatar asked Oct 25 '25 09:10

Ghilas BELHADJ


1 Answers

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.

like image 75
lpapp Avatar answered Oct 27 '25 22:10

lpapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!