これを行うコードがあります。という名前のメソッドprepareUI
により、UI にフィードされた検索結果をロードできるようになります。onClear
すでに表示されている結果をクリアする必要があるときに呼び出される名前のメソッド。そして、populateSearchResults
検索データを受け取り、UI をロードするという名前のメソッド。からの結果をクリアする必要があるため、データを保持するコンテナーは公開されているポインターですonClear
。
void MyClass::prepareSearchUI() {
//there may be many search results, hence need a scroll view to hold them
fResultsViewBox = new QScrollArea(this);
fResultsViewBox->setGeometry(28,169,224,232);
fSearchResultsLayout = new QGridLayout();
}
void MyClass::onClear() {
//I have tried this, this causes the problem, even though it clears the data correctly
delete fSearchResultContainer;
//tried this, does nothing
QLayoutItem *child;
while ((child = fSearchResultsLayout->takeAt(0)) != 0) {
...
delete child;
}
}
void MyClass::populateWithSearchesults(std::vector<std::string> &aSearchItems) {
fSearchResultContainer = new QWidget();
fSearchResultContainer->setLayout(fSearchResultsLayout);
for (int rowNum = 0; rowNum < aSearchItems.size(); rowNum++) {
QHBoxLayout *row = new QHBoxLayout();
//populate the row with some widgets, all allocated through 'new', without specifying any parent, like
QPushButton *loc = new QPushButton("Foo");
row->addWidget(loc);
fSearchResultsLayout->addLayout(row, rowNum, 0,1,2);
}
fResultsViewBox->setWidget(fSearchResultContainer);
}
問題は、onClear
内部的に呼び出す which を呼び出すとdelete
、表示されていたすべての結果が削除されることです。しかしその後、populateWithSearchesults
もう一度呼び出すとアプリがクラッシュし、スタック トレースはこのメソッドがクラッシュした場所として表示されます。
この問題を解決するにはどうすればよいですか?