如何在列表视图中设置选择?

时间:2013-04-03 01:03:55

标签: python qt pyqt

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

2 个答案:

答案 0 :(得分:2)

最后我自己想出来了,这是答案,假设你有一个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)

IMO不是很直接,你必须使用模型来创建一个索引对象,但我认为这是有道理的。

答案 1 :(得分:-2)

我猜你使用QStringListModel而不是QStringModel,假设你使用QListView.Here是代码

    model = new QStringListModel(this);

listView = new QListView;
listView->setModel(model);
listView->setSelectionMode(QAbstractItemView::MultiSelection);//you can select many selections;