[英]Qt remove nested layout
我在單個QVBoxLayout
嵌套了幾個QHBoxLayout
對象。 我已經研究了許多stackoverflow的問題和答案,但是還沒有找到一種完全刪除QScrollArea
小部件內容布局的QScrollArea
。 我所看到的所有答案僅使再次設置布局成為可能,但是當第二次設置布局時,對象仍然存在。
這是我正在使用的代碼:
QSignalMapper* sMap = new QSignalMapper(this);
QVBoxLayout* vBox = new QVBoxLayout();
outerVector = 0;
for (vector<vector<QPushButton*>>::iterator o_iter = buttonGrid.begin(); o_iter < buttonGrid.end(); o_iter++) {
int innerVector = 0;
QHBoxLayout* hBox = new QHBoxLayout();
for (vector<QPushButton*>::iterator i_iter = (*o_iter).begin(); i_iter < (*o_iter).end(); i_iter++) {
hBox->addWidget(buttonGrid.at(outerVector).at(innerVector));
sMap->setMapping(buttonGrid.at(outerVector).at(innerVector), ((outerVector * 100) + innerVector));
connect(buttonGrid.at(outerVector).at(innerVector), SIGNAL(clicked()), sMap, SLOT(map()));
innerVector++;
}
vBox->addLayout(hBox);
outerVector++;
}
ui->GameAreaWidgetContents->setLayout(vBox);
connect(sMap, SIGNAL(mapped(int)), this, SLOT(on_buttonGrid_clicked(int)));
現在,我有這個用於清除布局:
delete hBox;
delete vBox;
ui->GameAreaWidgetContents->layout();
清除窗口小部件內容的最佳,最有效的方法是什么?
更新 :我推斷GameAreaWidgetContents
是QScrollArea
。 要清除其布局管理器,您可以執行以下操作:
delete ui->GameAreaWidgetContents->layout();
vbox將不再是窗口小部件的布局管理器,並且所有嵌套的子代將由Qt育兒系統自動刪除。
如果此窗口小部件上已經安裝了布局管理器,則QWidget將不允許您安裝另一個。 必須先刪除現有的布局管理器(由layout()返回),然后才能使用新布局調用setLayout()。
我相信我已經解決了這個問題,這不是Qt問題,而是更多的是缺少清除vector<vector<QPushButton*>> buttonGrid
對象。 看起來好像沒有清除布局,因為附加的QPushButton
對象已添加到vector<vector<QPushButton*>>
對象上。
代表我這是一個相當菜鳥的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.