我有一段代码,其中有三个方法与此相关:一个名为
问题是,当我调用
我该如何解决这个问题?
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
时,其内部调用delete
,它确实删除了所有显示的结果。但在此之后,如果我再次调用populateWithSearchesults
,我的应用程序会崩溃,并且堆栈跟踪将此方法显示为发生崩溃的位置。我该如何解决这个问题?