ListView and changes in model

  • I using QDeclarativeView to display content of QList<my_class> in ListView. That working fine. But if data in QList<my_class> was been modified, QDeclarativeView don't display that changes. How to notify ListView about changes in model ?

  • It is not possible to notify ListView about changes in QList (but you can reassign property with QList and it will help you). If you want notify declarative ListView about C++ model changes you should create class with QAbstractListModel inherited and notify via standard model mechanisms.

  • If you exposed the QList via setContextProperty, you will need to set the list again every time the data in it changes.

    If you exposed the list via a Q_PROPERTY, you should emit the notify signal for that property every time the list data changes.

    Either way, it won't happen by magic. You need to somehow communicate that the list has changed. If you want it to happen 'by magic', then use the QAbstractListModel approach (where you still have to indicate that it has changed, but once you indicate it to the model it will automatically propagate to QML).

  • Thanks for explanations

  • There is a rather complete example for a folderlist in the docs:

  • whenever that data in the model changes make sure to call dataChanged() in the model.This will update the listView.

Log in to reply