从 QTreeWidget 中删除行(qt 编程)
从 QTreeWidget 中删除行(QTreeWidgetItem)的最佳方法是什么?
QTreeWidget 内容已通过以下方式设置:
myQTreeWidget->insertTopLevelItems(0, items); // items = QList<QTreeWidgetItem*>
然后我从 QList“项目”中删除一个项目,并尝试清除/重置 QTreeWidget
packList->clear();
packList->insertTopLevelItems(0, items);
但我的应用程序在这里崩溃了! 建议?
what's the best way to remove a row (QTreeWidgetItem) from a QTreeWidget?
The QTreeWidget content has been set by:
myQTreeWidget->insertTopLevelItems(0, items); // items = QList<QTreeWidgetItem*>
then I remove an item from my QList "items" and I try to clear/reset the QTreeWidget
packList->clear();
packList->insertTopLevelItems(0, items);
but my app crashes here!
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是,调用
packList->clear()
会删除树包含的树小部件项。 (请参阅有关QTreeWidget::clear()
的文档,其中包含有关在删除之前从树中删除的项目的注释。)您需要找到一种方法来删除这些项目,或者不与树分开维护它们的列表。稍微相关的一点是,如果您尝试跟踪树中的其他数据,我建议您尝试使用模型范例。 在重要的情况下,通常值得我花时间转换为该技术,而不是使用小部件/项目。
Your problem is that calling
packList->clear()
deletes the tree widget items contained by the tree. (See the documentation aboutQTreeWidget::clear()
, which includes a note about the items being removed from the tree before deleting.) You'll either need to find a way to remove the items, or not maintain a list of them separately from the tree.On a slightly-related note, if you are trying to keep track of other data along with the tree, I'd recommend you try to use the models paradigm. In non-trivial cases, it has usually been worth my while to convert to that technique, rather than using the widgets/items.
根据此文档所说,您应该能够执行以下操作it with:
Which returns 删除并返回所提供索引处的项目。
From what this documentation says, you should be able to do it with:
Which returns removes and returns the item at the supplied index.