延迟加载子项 Qtreeview [英] lazy loading child items Qtreeview

查看:62
本文介绍了延迟加载子项 Qtreeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在树视图中加载子项

I am trying to load child items in a treeview

+top
  parent1
  parent2
  parent3

首先,我只用父节点信息填充模型并将其附加到树视图,树看起来像上面一样.

To start off I populate the model with parent node information only and attach it to a treeview, the tree looks like above.

self.mytreeview=QtGui.Qtreeview()
self.model=QtGui.QStandardItemModel(self.mytreeview)

def initialise_model(self):
    '''Populate the model with parent nodes only'''
    #open file and process each line, each file line has the parent node info
    ....

    for file_name_entry in fileobject:
        parent_item=QtGui.QStandardItem(str(file_name_entry).strip())
        parent_item.setData("this is a parent",QtCore.Qt.TooltipRole)
        self.model.appendRow(parent_item)
    self.mytreeview.setModel(self.model)

此时父母都没有任何孩子的信息.我接下来要做的是更新一个父节点,当用户点击父节点时(及时或延迟加载父子信息).

At this point none of the parents have any child information. What I want to do next is to update a parent node, when the user clicks on the parent node(just in time or lazy loading of a parents child information).

所以当父节点被点击时,信号被发射

So when a parent node is clicked, and the signal is emitted

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked(QModelIndex)'), self.update_model)

update_model 代码应该去获取那个父节点的子节点,更新模型,刷新树并展开父节点.

update_model code should go fetch the children of that parent, update the model, refresh the tree and expand the parent node.

    def update_model(self,index):
        '''Update the model by adding children to selected/clicked parent node only'''
        parent_node=QtGui.QStandardItem(self.model.itemFromIndex(index))
        parent_item_index=index.row()
        #open the file called parent_node.txt and get all the child entries from the file...
        child_index=0
        for child_name_entry in parent_text_fileobject:
            child_item=QtGui.QStandardItem(str(child_name_entry).strip())
            child_item.setData("this is a child",QtCore.Qt.TooltipRole)
            parent_item.setChild(child_index,child_item)
            child_index=child_index+1
#Once all the children have been added update the model, by inserting a new row where the parent was
        self.model.removeRow(parent_item_index)#remove old parent
        self.model.insertRow(parent_item_index,parent_item)#insert new parent with its children

当我尝试删除选定的父节点(没有子节点的节点)并尝试插入一个新的父项(带有子节点)时,它最终会在没有子节点的父节点上方添加新的父节点(带有子节点),所以我最终得到重复的父节点

When I try to remove the selected parent node (the one with no children) and try inserting a new parent item (with children) it ends up addding the new parent(with children) above the parent node with no children so I end up with duplicated parent nodes

+top
  parent1
  parent2
  +parent2
    child_a
    child_b
    child_c  
  parent3

所以只是回顾一下,删除和插入即

So just to recap, removing and inserting i.e.

#Once all the children have been added update the model, by inserting a new row where the parent was
        self.model.removeRow(parent_item_index)#remove old parent
        self.model.insertRow(parent_item_index,parent_item)#insert new parent with its children

是个坏主意.查看评论,有人建议 AppendRow() 可能是答案,但我不太确定我相信

is a bad idea. Looking at the comments its been suggested that AppendRow() might be the answer, but I am not so certain I believe that

self.model.appendRow(parent_item)#new parent with its children

只需将父节点添加到我的模型底部,因此树仍然会有重复的父节点,即

Would simply add the parent to the bottom of my model and therefore the tree would still have duplicate parent nodes i.e

+top
  parent1
  **parent2**
  parent3
  **+parent2**
    child_a
    child_b
    child_c  

我认为我正在寻找一种更新模型中现有节点的方法,以便可以使用其子节点更新所选(父节点).例如

I think what I am looking for is a way to update an existing node in the model, so that the selected (parent node) can be updated with its children. e.g.

...
    self.model.UpdateExisitingNode(node_index)
...

关于如何更新模型中的节点有什么建议吗?注意我使用的是 Windows 和 PyQt

Any suggestions on how to update an node in model? Note I am using Windows and PyQt

问候

推荐答案

您正在复制项目,因为您正在创建另一个父项目,您应该只创建新的子项目.例如:

You are duplicating items because you are creating another parent item, you should only create new child items. For example:

from PyQt4 import QtGui


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.mytreeview = QtGui.QTreeView(self)
        self.setLayout(QtGui.QVBoxLayout())
        self.layout().addWidget(self.mytreeview)
        self.model = QtGui.QStandardItemModel(self.mytreeview)
        self.mytreeview.setModel(self.model)
        self.mytreeview.clicked.connect(self.update_model)
        self.initialise_model()

    def initialise_model(self):
        for text in ["parent1", "parent2", "parent3"]:
            item = QtGui.QStandardItem(text)
            self.model.appendRow(item)

    def update_model(self, index):
        parent = self.model.itemFromIndex(index)
        for text in ["children1", "children2", "children3"]:
            children = QtGui.QStandardItem("{}_{}".format(parent.text(), text))
            parent.appendRow(children)
        self.mytreeview.expand(index)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于延迟加载子项 Qtreeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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