使用QStandardItemModel在QListView中获取选定的索引 [英] Get selected index in QListView using QStandardItemModel

查看:1002
本文介绍了使用QStandardItemModel在QListView中获取选定的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建QListView和QStandardItemModel:

I am creating a QListView and QStandardItemModel using this code:

self.listView = QtWidgets.QListView(self.groupBox2)
self.listView.setGeometry(QtCore.QRect(200, 20, 400, 220))
self.entry = QtGui.QStandardItemModel()

我正在使用类似以下内容的循环添加项目:

I am adding items in a loop using something similar to:

self.item1 = QtGui.QStandardItem("Itemname1")
self.entry.appendRow(self.item1)
self.item2 = QtGui.QStandardItem("Itemname2")
self.entry.appendRow(self.item2)

如何知道列表中的0代表"Itemname1",1代表"Itemname2"等,如何将这些项目分配给列表?

How can I assign those items to a list knowing 0 in the list stands for "Itemname1", 1 stands for "Itemname2" etc.?

此外,如何将QListView中的项目设置为选中状态? QStandardItemModel似乎不存在setSelected()

Also, how can I set an item in the QListView as selected? setSelected() doesn't seem to exist for QStandardItemModel

推荐答案

QStandardItemModel是一个模型,因此您可以将QAbstractItemModel的所有方法用作rowCount()来告诉我们行数,因此您可以遍历它们,并使用item()方法获得与每个索引关联的QStandarItem,然后使用QStandarItemtext()方法获取文本.

QStandardItemModel is a model so you can use all the methods of QAbstractItemModel as rowCount() that tells us the number of rows, so you iterate over them and using the item() method you get the QStandarItem associated with each index, and then use the text() method of the QStandarItem to get the text.

l = [self.entry.item(i).text() for i in range(self.entry.rowCount())]
print(l)

另一方面,如果要选择从QAbstractItemView继承的视图元素,在这种情况下QListView,则必须使用与将使用方法QItemSelectionModel >. QItemSelectionModel有几种以不同方式选择和取消选择的方法,在这种情况下,您必须使用select方法并传递标志QItemSelectionModel::Select.另一方面,默认情况下,视图被启用以选择单个元素,如果要选择更多元素,则必须使用setSelectionMode()方法并将其传递给标志QAbstractItemView::MultiSelection.

On the other hand if you want to select an element of a view that inherits from QAbstractItemView as in this case QListView you must use the QItemSelectionModel associated with the view that will be obtained using the method selectionModel(). The QItemSelectionModel has several methods to select and deselect in different ways, in this case you must use the method select and pass the flag QItemSelectionModel::Select. On the other hand, by default the views are enabled to select a single element, if you want more elements to be selected you must use the setSelectionMode() method and pass it the flag QAbstractItemView::MultiSelection.

示例:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        lay = QtWidgets.QVBoxLayout(self)
        button = QtWidgets.QPushButton("to list")
        button.clicked.connect(self.modelToList)
        self.listView = QtWidgets.QListView()
        lay.addWidget(button)
        lay.addWidget(self.listView)

        self.entry = QtGui.QStandardItemModel()
        self.listView.setModel(self.entry) 

        for text in ("Itemname1", "Itemname2", "Itemname3", "Itemname4"):
            it = QtGui.QStandardItem(text)
            self.entry.appendRow(it)

        self.listView.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
        sm = self.listView.selectionModel()
        for item in (self.entry.item(1), self.entry.item(2)):
            index = self.entry.indexFromItem(item)
            sm.select(index, QtCore.QItemSelectionModel.Select)

    def modelToList(self):
        l = [self.entry.item(i).text() for i in range(self.entry.rowCount())]
        print(l)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())


如果只想选择一个项目,则必须使用setCurrentIndex(),因为它在内部调用selectionModel()select()方法.


If you only want to select an item then you must use setCurrentIndex() since it internally calls the select() method of selectionModel().

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from random import randint


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        lay = QtWidgets.QVBoxLayout(self)
        button = QtWidgets.QPushButton("to list")
        button.clicked.connect(self.modelToList)
        self.listView = QtWidgets.QListView()
        lay.addWidget(button)
        lay.addWidget(self.listView)

        self.entry = QtGui.QStandardItemModel()
        self.listView.setModel(self.entry) 

        for text in ("Itemname1", "Itemname2", "Itemname3", "Itemname4"):
            it = QtGui.QStandardItem(text)
            self.entry.appendRow(it)

        item = self.entry.item(randint(0, self.entry.rowCount()-1))
        self.listView.setCurrentIndex(self.entry.indexFromItem(item))

    def modelToList(self):
        l = [self.entry.item(i).text() for i in range(self.entry.rowCount())]
        print(l)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于使用QStandardItemModel在QListView中获取选定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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