Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert QWidget to an existing QBoxLayout at a particular position?

Tags:

qt

qwidget

The questions says it all. Say I have a layout

mylayout
|-- widgetA
`-- widgetB

How do I insert a new widget to this tree?

mylayout
|-- widgetA
|-- widgetC
`-- widgetB
like image 513
S B Avatar asked Jun 13 '12 13:06

S B


People also ask

How do I add a layout to Qwidget?

To set the layout on the parent widget, you need to drop the ->layout() : twInputMethodsTabs->value(1). first->addLayout(hblParams); Note that since you are now adding an empty layout to the widget, any widgets current in the previous layout will be lost, so you may need to re-add the widgets to the layout.

How do I add a button to layout QT?

You can add QPushbutton to central widget by passing its object name. QPushButton *PB=new QPushButton(ui->centralwidgetobjectname); PB. show(); Otherwise create layout inside Central widget then using addwidget function to add Qpushbutton.

What is stretch factor QT?

Stretch factors are used to change how much space widgets are given in proportion to one another. If we apply stretch factors to each widget, they will be laid out in proportion (but never less than their minimum size hint), e.g.

What is QVBoxLayout?

The QVBoxLayout class lines up widgets vertically. This class is used to construct vertical box layout objects. See QBoxLayout for details.


1 Answers

You should use QBoxLayout::insertWidget.

The index is zero-based,
it means the number of the widget before which the new one will be inserted.
Or, you can think of it as  what the index of the inserted item will become.

In this particular case you have:

layout.addWidget(widgetA)
layout.addWidget(widgetB)

layout.insertWidget(1, widgetC)
like image 99
Oleh Prypin Avatar answered Sep 24 '22 22:09

Oleh Prypin