Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt removing stretches from a QHBoxLayout

Tags:

c++

qt

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.

like image 936
ulak blade Avatar asked Jul 16 '15 13:07

ulak blade


People also ask

What is QBoxLayout?

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.

How do I add a layout to QWidget?

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.


1 Answers

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);
like image 62
Miki Avatar answered Sep 20 '22 04:09

Miki