c++ - 如何从 Qt 中的布局中删除小部件

标签 c++ qt pointers qlayout

我有一段代码可以执行此操作:名为 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);
}

问题是,当我调用内部调用 deleteonClear 时,它会删除所有显示的结果。但在那之后,如果我再次调用 populateWithSearchesults,我的应用程序就会崩溃,并且堆栈跟踪会显示此方法崩溃的位置。

我该如何解决这个问题?

最佳答案

看来您对所有权有一些误解。 QLayout 拥有添加到它的任何项目的所有权:http://doc.qt.io/qt-5/qlayout.html#addItem

这意味着 QLayout 负责删除这些项目。如果您删除它们,那么 QLayout 也会尝试删除它们,然后您就会遇到现在看到的崩溃。

QLayout 没有很好的删除内容和重新添加内容的功能(例如 removeWidget 可能不会像您希望的那样工作。)但这是有原因的。

QLayout 不打算用作 ListView 。

你想要的是一个,等等,QListView .它甚至会为您处理滚动功能,使添加和删除元素成为可能。

关于c++ - 如何从 Qt 中的布局中删除小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326090/

相关文章:

c++ - 结构数组的内存分配错误

c++ - 对我的 BFS tile 类中的引用和指针感到困惑

c# - c#中的托管指针数组

c++ - NXM矩阵的所有AXB子矩阵中的最大元素

c++ - 多重继承和指针实现

qt - 处理 QQuickItem 上的鼠标事件

qt - 在 qmake 中添加自定义目标

c++ - 改变 QPushButton 的数量

c++ - 为什么 OutputDebugString 为空

python - 在我的 Qt GUI 中包含外部小部件 [python]