如何在列表视图中设置选择? [英] How do I set the selection in a listview?

查看:46
本文介绍了如何在列表视图中设置选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 QStringModel 的列表视图,我想以编程方式将其更改为选择.我有一个 python 索引列表作为整数,[1,3,4],我想选择它.如何选择这些指数?列表视图是否具有允许我选择一行的功能?

I have a listview with a QStringModel and I want to change its to selection programatically. I have a a python list of indices as ints, [1,3,4], that I would like to select. How can I select these indices? Does the listview have a function that will allow me to select a row?

推荐答案

自己终于想通了,这里就是答案,假设你有一个listviewmyListview",对应的模型myQStringListModel"至少有6个元素(0-5) 和索引数组 [1,3,5]:

Finally figured it out myself, here is the answer, assuming you have a listview "myListview", a corresponding model "myQStringListModel" with at least 6 elements (0-5) and a array of indices [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)

IMO 不是很直接,您必须使用模型来创建索引对象,但我想这是有道理的.

IMO its not very straight forward that you have to use the model to create an index object, but it makes sense I guess.

这篇关于如何在列表视图中设置选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆