By default, the built-in views in PyQt can auto-refresh itself when its model has been updated. I wrote my own chart view, but I don't know how to do it, I have to manually update it for many times.
Which signal should I use?
You need to connect your view to the dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )
signal of the model:
This signal is emitted whenever the data in an existing item changes.
If the items are of the same parent, the affected ones are those between topLeft and bottomRight inclusive. If the items do not have the same parent, the behavior is undefined.
When reimplementing the setData() function, this signal must be emitted explicitly.
If you do not have the index() object from model , you can call the data changed signal from the model or outside the model, if you call from the model, follow the next step:
self.dataChanged.emit(index, index)
some models functions can get the index,for example:
def setData(self, **index**, value)
so, you can call the signal:
def setData(self, index, value):
#do something
self.dataChanged.emit(index, index)
The index on the signal indicates which element changed its data If you want to change, refresh or reload data from outside the model and you do not have the index, it is not necessary to indicate which element will be changed, so, you can apply the next line
self.yourmodel.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())
This will refresh all the data that has changed.