如何从其他QLayout中删除QLayout

How to remove QLayout from other QLayout?

本文关键字:QLayout 删除 其他      更新时间:2023-10-16

我有一个包含其他布局(sublayout)的布局。我需要从布局中删除包含内容的子层。我该怎么做?

QVBoxLayout* mainLayout = new QVBoxLayout; 
QHBoxLayout* subLayout = new QHBoxLayout; 
for(int i = 0; i < 3; i++)
   subLayout->addWidget(new QPushButton(this)); //some content of sublayout
mainLayout->addLayout(subLayout);
setLayout(mainLayout); 

这个类中只有QLayout::removeWidget(),但没有类似QLayout::removeLayout()的内容。只是delete subLayout

QLayoutItem *item;
while ((item = subLayout->takeAt(0)))
   delete item;
delete subLayout;

也没有正确的效果(内容仍然保留在屏幕上)。

那么怎么做呢?

void QLayout::removeItem(QLayoutItem *item)

从布局中删除布局项项目。这是来电者的删除项目的责任。

请注意,项可以是布局(因为QLayout继承了QLayoutItem)。