I am trying to get a custom ListModel to work and to be displayed in PyQt. However the list view always ends up blank. I checked that UserModel.users does have the items in it it should have, and dataChanged fires properly, however nothing shows up. At the same time, print(index) in data never outputs anything, so the model data never gets called? What am I missing?
self.userList = QtWidgets.QListView()
self.userList.setModel(self.main.commandHandler.userList)
self.userList.show()
class UserModel(QAbstractListModel):
def __init__(self, parent=None):
QAbstractListModel.__init__(self, parent)
self.users = []
def rowCount(self, parent = None) :
if parent != None:
return 0
return len(self.users)
def flags(self):
return Qt.NoItemFlags
def data(self, index, role = Qt.DisplayRole ):
print(index)
name = self.users[index].name
if index.isValid():
if (role == Qt.DisplayRole):
return QVariant(name)
else:
return QVariant()
def addUser(self, payload):
user = User(payload)
l = len(self.users)
self.users.append(user)
self.dataChanged.emit(self.index(l, 0), self.index(l, 0))