我不明白为什么这似乎没有扩展QTreeView中的顶级根项:
# clear existing treeview data
model = self.treeview.model().sourceModel()
model.clear()
# add treeview items here
# expand root level items
root = model.invisibleRootItem()
index = root.index()
for i in range(root.rowCount()):
item = model.indexFromItem(model.item(i,0))
self.treeview.expand(item)
self.treeview.setExpanded(item, True)
print 'expanded'
答案 0 :(得分:2)
如果您正在使用代理模型,则必须使用它提供的索引,而不是源模型中的索引。所以要么这样做:
proxy = self.treeview.model()
for row in range(proxy.rowCount()):
index = proxy.index(row, 0)
self.treeview.expand(index)
或者这个:
proxy = self.treeview.model()
model = proxy.sourceModel()
for row in range(model.rowCount()):
index = model.index(row, 0)
self.treeview.expand(proxy.mapFromSource(index))