If I click any row in the list, my data will be modified (wired setup just to replicate the external data update case). How to load updated data in my widget?
Probably I need to use layoutChanged signal, but I had no luck with this.
from PySide2 import QtCore, QtWidgets
class Project:
def __init__(self, project_name):
self.id = None
self.name = project_name
self.maya = ''
self.description = ''
class Model(QtCore.QAbstractListModel):
def __init__(self, data=[], parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self._data = data
def rowCount(self, parent):
return len(self._data)
def data(self, index, role):
if not index.isValid():
return
row = index.row()
value = self._data[row]
if role == QtCore.Qt.DisplayRole:
return value.name
if role == QtCore.Qt.UserRole:
return value
app = QtWidgets.QApplication([])
def update_data():
global data
data = [Project('Strawberry')]
data = [Project('Apple'), Project('Banana')]
model = Model(data)
list = QtWidgets.QListView()
list.setModel(model)
list.clicked.connect(update_data)
list.show()
app.exec_()