如何从 QTreeWidget 中删除顶级 QTreeWidgetItem?
如果顶级项目中没有子节点,我正在尝试删除顶级树小部件项目。这样做的正确方法是什么?我似乎无法在 Qt 的文档中找到 API 调用。仅在顶级树小部件项上调用删除是否安全?我还没有遇到任何问题,但我想知道这是否安全。非常感谢。
if(topLevelTreeWidgetItem->childCount() > 1) {
topLevelTreeWidgetItem->removeChild(childItem);
}
else
{
delete topLevelTreeWidgetItem;
}
I'm attempting to remove a top level tree widget item if there are no child nodes within the top level item. What is the correct way to do this? I can't seem to find the API call within Qt's documentation. Is it safe to just call delete on the top level tree widget item? I haven't run into any issues yet, but I'd like to know if that's safe practice. Thanks much.
if(topLevelTreeWidgetItem->childCount() > 1) {
topLevelTreeWidgetItem->removeChild(childItem);
}
else
{
delete topLevelTreeWidgetItem;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接
删除
QTreeWidgetItem是完全安全的。根据 ~QTreeWidgetItem() 的文档:
我在实践中对许多 QTreeWidgetItems 使用了
delete
并且效果很好。delete
ing a QTreeWidgetItem directly is perfectly safe.According to the documentation for ~QTreeWidgetItem():
I've used
delete
on many QTreeWidgetItems in practice and it works quite well.要删除顶级项目,请调用 QTreeWidget::takeTopLevelItem 方法并然后删除返回的项目:
其中index是要删除的项目的索引。
To delete a top level item call QTreeWidget::takeTopLevelItem method and then delete the returned item:
Where index is index of the item to be removed.
函数 takeChild 仅适用于 QTreeWidgetItem。对于 QtreeWidget,您可以使用 QtreeWidget::takeTopLevelItem(int index)
Function takeChild only works with QTreeWidgetItem. With QtreeWidget, you can use QtreeWidget::takeTopLevelItem(int index)