如何在listview中设置选择?

2024-09-30 16:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有QStringModel的listview,我想通过编程将其更改为selection。我有一个python索引列表(ints,[1,3,4]),我想选择它。如何选择这些索引?listview是否有一个允许我选择行的函数?在


Tags: 函数列表编程listviewselectionintsqstringmodel
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)

在我看来,使用模型创建索引对象并不是很直接,但我想这是有意义的。在

我猜您使用的是QStringListModel而不是QStringModel,假设您使用的是QListView。这里是密码吗

    model = new QStringListModel(this);

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

相关问题 更多 >