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)
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
.
Example:
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_())
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_())