f.r*_*ues 4 python model-view-controller pyqt qtreeview pyside
我很难理解如何使用QTreeView和QStandardItemModel设置多级QTree.
这就是我所拥有的:
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_())
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样:

到目前为止只显示'root'项,它不会抛出任何错误.
另外,我把这个for循环变得有点匆忙,它很难阅读/理解,因为它有太多嵌套for循环.我想知道是否有更好的方法来扩展基于初始字典的树.
您正在为每个项目创建模型.那是错的.您应该.appendRow/.insertRow在项目上使用添加子项目.
至于你的循环,我可能会使用递归方法来填充树.这是我要做的:
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_())
Run Code Online (Sandbox Code Playgroud)
PS:你也没有在主窗口中使用任何布局.我在上面添加了它.
PSv2:如果可能的话,最好避免在python中进行类型检查,但这是你构建自己的方式所必需的tree.如果你以不同dict的方式构建它会更好,就像所有方式一样:
tree = {'root': {
'1': {'A':{}, 'B':{}, 'C':{}},
...
}
Run Code Online (Sandbox Code Playgroud)