简体   繁体   English

如何扩展顶级QTreeview项

[英]How to expand top-level QTreeview items

I do not understand why this does not seem to expand the top-level root items in a QTreeView: 我不明白为什么这似乎没有扩展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'

If you're using a proxy model, you must use the indexes it provides, rather than the ones from the source model. 如果使用代理模型,则必须使用它提供的索引,而不要使用源模型的索引。 So either do this: 因此,要么这样做:

proxy = self.treeview.model()

for row in range(proxy.rowCount()):
    index = proxy.index(row, 0)
    self.treeview.expand(index)

or this: 或这个:

proxy = self.treeview.model()
model = proxy.sourceModel()    

for row in range(model.rowCount()):
    index = model.index(row, 0)
    self.treeview.expand(proxy.mapFromSource(index))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM