python - 如何扩展顶级QTreeview项目

标签 python pyside expand 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'

最佳答案

如果您使用代理模型,则必须使用它提供的索引,而不是源模型中的索引。所以要么这样做:

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))

关于python - 如何扩展顶级QTreeview项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596847/

相关文章:

pyqt - 当从另一个 QMainWindow 调用时 QMainWindow 闪烁并消失

html - 如何在悬停时展开 div 及其内容?

python - `readline` 、 `readlines` 、 `writeline` 和 `writelines` 是否需要 `file` 对象 `f` 以文本模式而不是二进制模式打开?

python - 如何使用 pytest 将 fixture 中的对象共享到所有测试?

python - 使用 Python 结合 asyncio 进行 Web 抓取

c# - 针对嵌套 OData 创建动态 linq 表达式树

模拟 LCARS(星际迷航)娱乐网站的 css 可变宽度问题

python - NavigationLayout Kivy err0r

python - PySide 中缺少 QtGui.QIdentityProxyModel?

python - PySide 导入错误 Mac OS X El Capitan, Library not loaded : @rpath/libpyside. cpython-34m.1.2.dylib