- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
有没有办法使 QTreeWidget
反射(reflect)对内部数据结构(如字典)所做的更改?看起来他们会在 api 中创建此功能,因为有许多程序可能与来自多个 GUI 区域的 QTreeWidget
交互,但 QTreeWidget
所需的主要目的> 是展示任意时间点的数据结构。 QtGui
项的文档对我来说不是那么容易掌握,因为它通常指的是 C 文档,而且我不确定它如何转移到 python。
所以基本上我想要的是使 QTreeWidget
显示嵌套字典的最简单方法,其中顶级对应于键,子级对应于值。此外,如果值是字典,则使用该级别中的键并为值创建子级别等。
这很容易做到吗?我还没有找到任何东西来做像这样的数据结构的简单镜像。
最佳答案
这是一个简单的实现:
def fill_item(item, value):
item.setExpanded(True)
if type(value) is dict:
for key, val in sorted(value.iteritems()):
child = QTreeWidgetItem()
child.setText(0, unicode(key))
item.addChild(child)
fill_item(child, val)
elif type(value) is list:
for val in value:
child = QTreeWidgetItem()
item.addChild(child)
if type(val) is dict:
child.setText(0, '[dict]')
fill_item(child, val)
elif type(val) is list:
child.setText(0, '[list]')
fill_item(child, val)
else:
child.setText(0, unicode(val))
child.setExpanded(True)
else:
child = QTreeWidgetItem()
child.setText(0, unicode(value))
item.addChild(child)
def fill_widget(widget, value):
widget.clear()
fill_item(widget.invisibleRootItem(), value)
我添加了列表支持以防万一有人需要它。
用法:
d = { 'key1': 'value1',
'key2': 'value2',
'key3': [1,2,3, { 1: 3, 7 : 9}],
'key4': object(),
'key5': { 'another key1' : 'another value1',
'another key2' : 'another value2'} }
widget = QTreeWidget()
fill_widget(widget, d)
widget.show()
结果:
关于python - QTreeWidget 镜像 python 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21805047/
我有一个 GUI,用户可以从列表中选择气体成分并将其移至“选择”,另一个按钮从“选择”中获取包含以下列的文本:气体成分、分子量、摩尔%。前两列从我创建的字典中获取信息,最后一列是用户输入。 当点击按钮
我的 QT 应用程序中有两个 QTreeWidget,使用 python (PyQt4)。 我想要 手动将 TreeWidget 1 展开为一个项目,然后。如果此项目位于 TreeWidget 2 中
我有两列的 QTreeWidget。第一列是文本,第二列是 QPushButton。我无法指定按钮的大小和第二列的大小。当您尝试设置内容栏的大小时,第二栏消失了。如何改变第二列的宽度? tree_wi
我有两列的 QTreeWidget。第一列是文本,第二列是 QPushButton。我无法指定按钮的大小和第二列的大小。当您尝试设置内容栏的大小时,第二栏消失了。如何改变第二列的宽度? tree_wi
我需要在 QTreeWidget 中的项目激活时执行一些操作,但下面的代码没有给我预期的结果: class MyWidget(QTreeWidget): def __init__(self,
我有一个文件,其中列出了绝对文件路径,每行 1 个。列出的文件是按顺序排列的,因此例如所有文件/Documents/ dir 将在文件中列在彼此之后。 我想要做的是将所有这些文件放在 QTreeWid
我想在我的程序中实现一个带有嵌套子级别的树,我正在寻找这两种( View /小部件)中的哪一种最适合我的目标。 我有一个任务完成/错过/失败的天数列表,每个任务都有完成/错过/失败的次数,最后是当天的
有谁知道是否可以在 QTreeWidget 上选择多个项目以及如何启用多项选择? 我想要选择的所有项目都是顶级 QTreeWidgetItems 并且它们的所有子项都设置为禁用(即 QTreeWidg
我对 Python 还很陌生,所以请耐心等待。我正在尝试从元组填充 QTreeWidget,但我似乎无法让它正常工作。 TreeList = ({ 'Header1': ((
我想不显示父 QWidgetItem 之间的空格和子项...我该怎么做。 style-Sheet ?.. 谢谢你!!! 这是 QTreeWidget . 最佳答案 http://qt-project.
我正在开发 python 插件。我使用 PYQT4 设计器来制作表单。我将查询结果列出到 QTreeWidget 中,如下所示: items = [] for row in c.f
我的 Qt 表单中有一个 treewidget。它显示了一个文件树,显示了一个代表它们的图标,以及它们的名称。 我使用 treeItem->setIcon(0, *icon) 和 treeItem->
我有一个 QTreeWidget,我的设置是这样的...... ModelTreeWidget::ModelTreeWidget(QWidget *parent):QTreeWidget(parent
默认情况下,QTreeWidget 管理行的选择(当您单击一行时它会突出显示它,当您单击另一行时它会突出显示并取消选择前一行) ,我不想要这个,也不知道如何关闭它。 最佳答案 你可以使用setSele
当我将选择模式设置为 ExtendSelection 时,“选定效果”(使用 TAB 时获得的效果)始终存在于我的 QTreeWidget 中(我需要它才能选择多个项目)。 如果我从代码中删除这一行,
我有一个 QTreeWidget,其中填充了自定义小部件。我从外部 API 检索项目类型,它可能是文本值、数值或其他任何值。根据类型,我为 QTreeWidgetItem 提供了不同的控件。例如用于文
我有一个 QTreeWidget,我想添加图标,是否有默认设置可供选择,例如资源管理器中的文件夹和一张空白纸图标? 最佳答案 默认有几个图标,例如: 与 QStyle::standardIcon(..
嗯。我正在开发一个 IDE。你们中的一些人可能看到了一篇关于它的帖子。好吧,我不知道 QTreeWidget 和 QtreeWidgetItem 是如何工作的,因为找不到演示并且文档也没有帮助。好吧,
我想要一个带有 1 列标题的 qtreewidget,但它的子项有更多列,我尝试了下面的代码,但我也想隐藏补贴标题。 treeView = new QTreeView; treeView->setMo
我正在尝试进行某种递归。如果您打开 P,那么您可以再次打开 P 或执行其他操作。这是我的问题: 我不能两次添加相同的项目。 while (*it) { if ((*it)->text(0) =
我是一名优秀的程序员,十分优秀!