我正在使用 pyqt5 和 python 3.6。我对 QModel/TreeView 中的索引函数感到困惑,并要求在这里清除我的大脑结。有些线程更让我困惑,因为它们非常特别。我的问题或多或少是一个基本问题。
我有一个带有数据的模型(基于Uludag's great tutorials)并将其设置到我的树中:
treeView = QTreeView()
treeView.show()
treeView.setModel(model)
#additional code
treeView.clicked.connect(treeViewStructure_clicked)
现在我必须根据所选项目在程序中准备进一步的操作。我可以通过
def treeViewStructure_clicked(self):
#get the row of the click and print
index = treeView.currentIndex()
print(index.row())
print('selected item index at %s with data: %s' % (index.row(), index.data()))
到目前为止,一切都很好。但是索引是从 treeView 生成的(模型不起作用)。如何将视图中的选择连接到模型,以便我拥有正确的模型索引并可以更改模型中的数据?当然,如何将它们写回以使它们同步?换句话说,我需要哪个索引来说明它们来自什么、来自哪里以及来自哪里?