0
我想扩展一个问题,我之前问过一个过滤器。在我previous question 我有帮助延迟加载树视图,父母第一,只增加了父母的孩子,当用户点击的节点上或多或少如下延迟使用QsortFIlterProxymodel过滤QTreeview
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)
我现在想要扩展和利用的过滤器,以便用户可以键入父节点的名称,并且树视图将自身过滤到合适的相关节点。我仍然想保持用户点击父节点的能力,并且即使只有在过滤时才会将子节点添加到父节点。
我该如何适应以下情况?我已经设置了树状了如下
proxyModel = QSortFilterProxyModel(treeView)
proxyModel.setSourceModel(self.model)
# set model
treeView.setModel(proxyModel);
treeView.setSortingEnabled(true)
def update_model(self, index):
parent = self.model.itemFromIndex(index)
##not sure about this now in the light of the proxyModel
for text in ["children1", "children2", "children3"]:
children = QtGui.QStandardItem("{}_{}".format(parent.text(), text))
parent.appendRow(children)
self.mytreeview.expand(index)#not sure about this either as the index is of the proxyModel
我首先两个主要问题proxyModel指数和源模型指标,我不知道这是如何工作以及当扩大点击节点,