从 TreeModel 中的选定索引检索特定数据
retrieve a specific data from the selected index in a TreeModel
我使用自定义模型来填充 TreeView
并尝试使用
for ix in self.dataView.selectedIndexes():
text = ix.data() # or ix.data()
print(text)
but this prints all the data in that column(index)...here is a picture of the model i used model = QStandardItemModel(0, 3, parent)
,
我的问题是我不需要所有数据,我需要第 3 行的数据(特定数据),即文件路径
这是输出,使用后面的代码
We & Love.txt
11.630%
C:\Users\Black Laptop\Desktop\Work\We & Love.txt
我只需要第三个数据,不用全部,谢谢
QModelIndex 与每个项目相关联,在您的情况下,您有完整的行之一,因此解决方案是按列过滤:
for ix in self.dataView.selectedIndexes():
# the indexes of the column start at 0 so the 3rd column has index 2
if ix.column() == 2:
text = ix.data()
print(text)
我使用自定义模型来填充 TreeView
并尝试使用
for ix in self.dataView.selectedIndexes(): text = ix.data() # or ix.data() print(text)
but this prints all the data in that column(index)...here is a picture of the model i used
model = QStandardItemModel(0, 3, parent)
,
我的问题是我不需要所有数据,我需要第 3 行的数据(特定数据),即文件路径
这是输出,使用后面的代码
We & Love.txt
11.630%
C:\Users\Black Laptop\Desktop\Work\We & Love.txt
我只需要第三个数据,不用全部,谢谢
QModelIndex 与每个项目相关联,在您的情况下,您有完整的行之一,因此解决方案是按列过滤:
for ix in self.dataView.selectedIndexes():
# the indexes of the column start at 0 so the 3rd column has index 2
if ix.column() == 2:
text = ix.data()
print(text)