有时候为了界面好看需要增加弹簧,这时候用addStretch,是没有保留指针的,想把弹簧删除就遇到麻烦了,这时候可以使用spacerItem这个方法,去移出弹簧,代码如下。
for (int idx = 0; idx < horizontalLayoutWidget->count(); ++idx)
{
QLayoutItem *layoutItem = horizontalLayoutWidget->itemAt(idx);
if (horizontalLayoutWidget->spacerItem())
{
horizontalLayoutWidget->removeItem(layoutItem);
--idx;
}
}
代码摘抄自:https://www.jianshu.com/p/daf9c035f883
转载请先经过原作者同意。