Search code examples
pythonqtuser-interfaceqt4pyqt

PyQt - Automatically refresh a custom view when the model is updated?


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?


Solution

  • 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.