删除 PyQt 中的 QTreeWidgetItem? [英] Delete QTreeWidgetItem in PyQt?

查看:27
本文介绍了删除 PyQt 中的 QTreeWidgetItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难找到一种简单的方法来删除我选择的 QTreeWidgetItem.

I'm finding it frustratingly hard to find a simple way to delete my selected QTreeWidgetItem.

我的拼凑方法包括将树的当前选择设置为 current 然后:

My patchwork method involves setting the tree's current selection to current and then:

if current.parent() is not None:
   current.parent().removeChild(current)
else:
   self.viewer.takeTopLevelItem(self.viewer.indexOfTopLevelItem(current))

这并不可怕,但不是有一个直接删除项目的命令吗?

It's not horrible, but isn't there a command that straight up just removes the item?

推荐答案

QTreeWidget 类有一个 invisibleRootItem() 函数允许更简洁的方法:

The QTreeWidget class has an invisibleRootItem() function which allows for a somewhat neater approach:

root = tree.invisibleRootItem()
for item in tree.selectedItems():
    (item.parent() or root).removeChild(item)

这篇关于删除 PyQt 中的 QTreeWidgetItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆