after adding a stretch with QBoxLayout::addStretch
, can I then somehow remove it? I want to keep adding a stretch at the end of the layout after every widget I add, so I have to remove the old stretch, add the new widget and add a new stretch after that.
QBoxLayout takes the space it gets (from its parent layout or from the parentWidget()), divides it up into a row of boxes, and makes each managed widget fill one box. If the QBoxLayout's orientation is Qt::Horizontal the boxes are placed in a row, with suitable sizes.
Layouts can be applied on QWidgets only. What you have to do is to insert a widget and apply the new layout to the widget. Tab widgets can contain the layout to align all items in it. In example above i add widget on this layout - it works fine.
Instead of removing and adding the stretch at the end, you could start with a layout containing only the stretch.
Then, instead of adding the new widget, you insert it at position layout->count()-1
. The stretch will shift one position to the right.
So:
QHBoxLayout* layout = new QHBoxLayout();
layout->addStretch();
setLayout(layout);
...
layout->insertWidget(layout->count()-1, yourWidget);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With