marmistrz
marmistrz

Reputation: 6414

A dynamic C++ model and a QML ListView

I'm using a QML list view which displays one element at a time

 ListView
 {
      model: cppobj.list
      ...
 }

cppobj is a C++ object which can be modified, i.e. items can be removed, appended, etc. If an element is appended, the ListView goes back to the first element. What's more ListView.onRemove is not called. Any ideas how to cope with it?

Thanks

/edit: the append function of the C++ object looks like that:

void append (QString str) { m_list.append(str); emit listChanged(m_list); }

Upvotes: 3

Views: 7152

Answers (2)

GDevT
GDevT

Reputation: 241

In case you want to have a ListModel for variant JSON data that you can use directly in QML, you can have a look at the JsonListModel. It can synchronize JSON data to a ListModel so you do not lose the current scroll position of the list. You can also apply transition animations and have the full ListView/ListModel features available.

 ListView
 {
      model: JsonListModel {
          source: myJsonData
          keyField: "id"
      }
      ...
 }

You can find a detailed guide how to use the JsonListModel here:

Upvotes: 0

Oleg Shparber
Oleg Shparber

Reputation: 2752

You need to use QAbstractListModel. See documentation here.

Upvotes: 3

Related Questions