多级QTreeView [英] Multi-level QTreeView
问题描述
我很难理解如何使用QTreeView和QStandardItemModel设置多级QTree.
I'm having a hard time understanding how to set a multilevel QTree using the QTreeView and QStandardItemModel.
这就是我所拥有的:
from PySide.QtGui import *
import sys
class MainFrame(QWidget):
def __init__(self):
QWidget.__init__(self)
tree = {'root': {
"1": ["A", "B", "C"],
"2": {
"2-1": ["G", "H", "I"],
"2-2": ["J", "K", "L"]},
"3": ["D", "E", "F"]}
}
self.tree = QTreeView(self)
root_model = QStandardItemModel()
self.tree.setModel(root_model)
for r,root in enumerate(sorted(tree)):
root_item = QStandardItem(root)
root_model.setItem(r,root_item)
for c,child in enumerate(sorted(tree[root])):
child_model = QStandardItemModel(root_model)
child_item = QStandardItem(child)
child_model.setItem(c, child_item)
for gc, grand_child in enumerate(sorted(tree[root][child])):
grand_child_model = QStandardItemModel(child_model)
grand_child_item = QStandardItem(grand_child)
grand_child_model.setItem(gc,grand_child_item)
if type(tree[root][child]) == dict:
for ggc, gran_grand_child in enumerate(sorted(tree[root][child][grand_child])):
gran_grand_child_model = QStandardItemModel(grand_child_model)
gran_grand_child_item = QStandardItem(gran_grand_child)
gran_grand_child_model.setItem(ggc, gran_grand_child_item)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainFrame()
main.show()
sys.exit(app.exec_())
我希望它看起来像这样:
I want it to look like this:
到目前为止,仅显示"root"项,它没有引发任何错误.
So far only the 'root' item is showing, it's not throwing any error.
此外,我急忙制作了这个for循环,很难读/理解,因为它嵌套了太多的for循环.我想知道是否有更好的方法可以根据初始命令扩展树.
Also, I made this for-loops in a bit of hurry, it's kind hard to read/understand because it has too many nested for loops. I wonder if there's a better way to expand the tree based on the initial dict.
推荐答案
您正在为每个项目创建一个模型.错了您应该在某项上使用.appendRow/.insertRow
来添加子项.
You're creating a model for each item. That's wrong. You should use .appendRow/.insertRow
on an item to add child items.
对于您的循环,我可能会使用递归方法来填充树.这是我要做的:
As for your loops, I'd probably use a recursive method to populate the tree. Here is what I'd do:
from PySide.QtGui import *
import sys
import types
class MainFrame(QWidget):
def __init__(self):
QWidget.__init__(self)
tree = {'root': {
"1": ["A", "B", "C"],
"2": {
"2-1": ["G", "H", "I"],
"2-2": ["J", "K", "L"]},
"3": ["D", "E", "F"]}
}
self.tree = QTreeView(self)
layout = QHBoxLayout(self)
layout.addWidget(self.tree)
root_model = QStandardItemModel()
self.tree.setModel(root_model)
self._populateTree(tree, root_model.invisibleRootItem())
def _populateTree(self, children, parent):
for child in sorted(children):
child_item = QStandardItem(child)
parent.appendRow(child_item)
if isinstance(children, types.DictType):
self._populateTree(children[child], child_item)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainFrame()
main.show()
sys.exit(app.exec_())
PS :您还没有在主窗口中使用任何布局.我在上面添加了它.
PS: You're also not using any layouts for the main window. I added it above.
PSv2 :最好在python中避免类型检查,但这是构造tree
的一种必要方式.如果您以不同的方式构造它会更好,例如dict
可能一直这样:
PSv2: Type-checking is best avoided in python, if possible, but it's kind of necessary the way you structured your tree
. It'd be better if you structured it differently, like dict
s all the way maybe:
tree = {'root': {
'1': {'A':{}, 'B':{}, 'C':{}},
...
}
这篇关于多级QTreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!