Using Python3 and pyside. I have a python dictionary that I want to display as a tree using Qt. I want the values โโto be editable, but not keys. I managed to achieve this using setItemWidget , as shown in the following example:
#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PySide import QtGui def data_to_tree(parent, data): if isinstance(data, dict): parent.setFirstColumnSpanned(True) for key,value in data.items(): child = QtGui.QTreeWidgetItem(parent) child.setText(0, key) data_to_tree(child, value) elif isinstance(data, list): parent.setFirstColumnSpanned(True) for index,value in enumerate(data): child = QtGui.QTreeWidgetItem(parent) child.setText(0, str(index)) data_to_tree(child, value) else: widget = QtGui.QLineEdit(parent.treeWidget()) widget.setText(str(data)) parent.treeWidget().setItemWidget(parent, 1, widget) app = QtGui.QApplication(sys.argv) wid = QtGui.QTreeWidget() wid.setColumnCount(2) wid.show() data = { 'foo':'bar', 'bar': ['f', 'o', 'o'], 'foobar':0, } data_to_tree(wid.invisibleRootItem(), data) sys.exit(app.exec_())
This works, but it contradicts what the documentation advises (static content) and makes it impossible to pre-create a widget (for example, in a separate thread) and then add it to the tree. Is there a better way to achieve what I want? The documentation mentions QTreeView, but I have not found a single example / tutorial that would allow me to understand how this will help me use my own widget in a column.
python qt pyside qtreeview qtreewidget
Neck
source share