I recently started rewriting an application and I'm trying to port it to model/view to reduce the number of kludges I have there.
So far I was able to succcessfully make a read-only model inheriting from QAbstractTableModel
. This model is something like this:
class MyModel(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
super(MyModel, self).__init__(parent)
self.data = data
data
is a list that contains a number of objects. These are then accessed in the data() method:
def data(self, index, role):
# much stuff omitted for clarity
return QtCore.QVariant(self.data[index.column()].id)
Now this is fine if I work with a predefined data
element. But in fact data
changes programmatically (it expands when certain signals are received). How can I keep the model aware of this, so that then my view can also react to these changes?
I've been reading about read-write models but they also allow the user to edit and change things, while in my view I want things to be un-editable: in short, the model would need to be changed "behind the scenes" only, and the view adapt to that.
What is the best appproach in this case? Implement a read-write model with setData() and so on, or is there a simpler solution?