QT5.4删除和删除布局中的小部件 [英] QT5.4 remove and delete widget in a layout
问题描述
我有一个QStackedLayout
,上面只有几个QWidget
.窗口小部件布局为QVBoxLayout
,其中也只有几个QPushButton
.我想做的是删除QStackedLayout
内的所有小部件,然后删除小部件和布局但我希望不要删除所有按钮,因为稍后我会将它们放到另一个小部件中
I have a QStackedLayout
which contains few QWidget
on it. The widgets layout is QVBoxLayout
which have also few QPushButton
. What I wanted to do is to remove all the widgets inside the QStackedLayout
and then delete the widgets and layouts BUT I want all the buttons not to be deleted 'cause I will put them to another widget later.
这是我的代码:
while (QLayoutItem *item = m_stacked_layout->takeAt(0)) {
QWidget *w = item->widget();
for (int i = 0; i < w->layout()->count(); i++) {
QPushButton *button = qobject_cast<QPushButton *>(w->layout()->itemAt(i)->widget());
if (button) {
w->layout()->removeWidget(button);
}
}
delete w;
delete item;
}
应用程序在delete w;
行崩溃.而且,如果我删除delete
行,则应用程序可以正常运行.
The application crashes at the line delete w;
. And, if I remove the delete
lines, application works fine.
推荐答案
但是我不想删除所有按钮,因为我会将它们放到 稍后再添加另一个小部件.
BUT I want all the buttons not to be deleted 'cause I will put them to another widget later.
- 隐藏所有要传输的小部件
- 将所有这些小部件的父小部件设置为
nullptr
- 稍后...设置必要的父项并显示小部件
注意:如果要删除某些插槽内的小部件,则应使用deleteLater
方法.
Note: if you want to delete widgets inside some slots, you should use deleteLater
method.
这篇关于QT5.4删除和删除布局中的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!