有時候爲了界面好看需要增加彈簧,這時候用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
轉載請先經過原作者同意。