PDA

View Full Version : Move QtLayout with child widgets



SvenA
16th June 2019, 23:07
Hello!

Is it possible (easily) to move an layout (QHBoxLayout) with child elements (QButton and QComboBox) from an QGridLayout into another layout (here an QVBoxLayout)?

I tried an grid_layout->removeItem(x) and an vbox_layout->insertItem(1,x), but it does not work.
Any ideas?

Regards
Sven

Ginsengelf
17th June 2019, 07:32
Hi, which part does not work? Removing or inserting?
Have you tried calling update() after the operation?

Ginsengelf

SvenA
17th June 2019, 08:31
Hello!

The code compiles and there is no error during execution.
Here some code:



QGridLayout* const glayout = dynamic_cast<QGridLayout*>(form->layout());
if (glayout)
{
QLayoutItem* const last_item = glayout->itemAtPosition(glayout->rowCount()-1, 0);

if (last_item && dynamic_cast<QHBoxLayout*>(last_item))
{
qDebug() << "Moving...";
glayout->removeItem(last_item);
ui->verticalLayout_frame_edit->insertItem(1, last_item);
}
}
}

Should this be enough to move the layout with all its children or do I have to do something special
to move the children too?

And I tried several "update()" calls...

Regards
Sven

ChrisW67
23rd June 2019, 08:05
The widgets that the layout controls the positioning for are children of the widget that layout was applied to, not the layout itself. If the two layouts are not in the same parent widget then you will need to consider this as well.