我有一个带有 QStringModel 的 ListView ,我想以编程方式将其更改为选择。我有一个 python 索引列表作为整数 [1,3,4],我想选择它。如何选择这些指数? ListView 是否具有允许我选择一行的功能?
最佳答案
我自己终于弄明白了,这就是答案,假设你有一个 ListView “myListview”,对应的模型“myQStringListModel”至少有 6 个元素 (0-5) 和一个索引数组 [1,3,5 ]:
theIndices = [1,3,5]
theQIndexObjects = [self.myQStringListModel.createIndex(rowIndex, 0, self.coverages_lm) for rowIndex in theIndices]
for Qindex in theQIndexObjects:
myListview.selectionModel().select(Qindex, QtGui.QItemSelectionModel.Select)
在我看来,您必须使用模型来创建索引对象并不是很直接,但我想这是有道理的。
关于python - 如何在 ListView 中设置选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777159/