QList 何时不能被 QVector 替换

问题描述

reading 之后 QVector 应该是 Qt 中容器的首选(超过 QList),我高兴地将代码中的一些 QList 更改为 { {1}} 然后继续我的工作,几乎忘记了这些变化......直到我开始遇到以前没有发生过的奇怪的崩溃。

崩溃发生在 QVector 类实现的本地副本中,我将其重命名为 QQuickTreeModelAdaptor1,它用于在 QML 视图中显示树状 C++ 模型。

在标题中,我替换了:

TreeModelAdaptor

    QList<TreeItem> m_items;
    QList<TreeItem *> m_itemsToExpand;

并在方法 QVector<TreeItem> m_items; QVector<TreeItem *> m_itemsToExpand;

TreeModelAdaptor::modelRowsAboutToBeMoved()

作者:

const QList<TreeItem> &buffer = m_items.mid(startIndex,totalMovedCount);

我不明白为什么会发生崩溃,然后我阅读了关于 QVector doc page 的以下说明:

const QVector<TreeItem> &buffer = m_items.mid(startIndex,totalMovedCount);

那时我想起了我所做的更改,并恢复到 Note: Iterators into a QLinkedList and references into heap-allocating QLists remain valid as long as the referenced items remain in the container. This is not true for iterators and references into a QVector and non-heap-allocating QLists. 。在那之后,崩溃似乎消失了。

我所经历的崩溃是否可以根据上面的注释来理解? source code 在这里发布有点太多了,但如果有人能指出上述注释在代码中的位置和方式,我将不胜感激?

此外,鉴于上述情况,Qt6 中引入的 unification between QList and QVector 怎么样,因为它说:

QList

这是否意味着以上代码无论如何都会在 Qt6 中崩溃?

编辑:正如@jarman 正确建议的那样,崩溃发生在以下 QVector is used as the underlying implementation. 几乎。如果我展开一个项目,然后在其他分支上做一些其他的展开,等等,并在通常发生的时候打开/关闭展开。

ASSERT

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...