Discussion:
dynamically updatable model in QML ListView
vineeth
14 years ago
Permalink
Hello all,
I am a newbie and had a doubt related to QML ListView model. In this
example :
http://doc.qt.nokia.com/4.7/declarative-modelviews-stringlistmodel.html , a
C++ QStringList is being used as a model in the view.qml
file<http://doc.qt.nokia.com/4.7/declarative-modelviews-stringlistmodel-view-qml.html>
.
However, If I add an item to this QStringList ( For ex: In response to a
UI click event ), the newly added items to the QStringList are not
reflected in the UI ListView. How can we ensure that the QStringList
elements are in sync with the QML ListView. Kindly let me know.

Thanks.
--vineeth
Pelle Johnsen
14 years ago
Permalink
You need to use QML ListModel or C++ QAbstractListModel, for changes to be
picked up by ListView.

ListModel is a lot easier if you are not used to Qt's item views.

http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html

<http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html>Rgrds,

-Pelle
...
Thomas Ganshorn
14 years ago
Permalink
i read once that you have to reassign the modell to the listview.
Not quite good implementation but there seems to be no other solution unless you
want to write your own qaim model (wich is something i really hate and would
always avoid if possible)


 
...
Pelle Johnsen
14 years ago
Permalink
If using ListModel you don't need to reassign the whole model (which is bad
as it requires ListView to recreate or at least reinitialize all delegates).

ListModel has methods for updating the model: append, insert, move, remove,
clear. I'm guessing it's using the item views mechanism under the hood to
inform ListView of the changes.

-Pelle
Post by Thomas Ganshorn
i read once that you have to reassign the modell to the listview.
Not quite good implementation but there seems to be no other solution
unless you want to write your own qaim model (wich is something i really
hate and would always avoid if possible)
vineeth
14 years ago
Permalink
I could get this working using :
declView->rootContext()->setContextProperty(...)
to update the model variable used in QML ListBiew, but I think this involves
unnecessary computation.
A more optimal way will be to get a reference to the model of the ListView
and call append on it.
There is a example of this in :
C:\QtSDK\Examples\4.7\declarative\modelviews\listview\dynamiclist\qml\ and I
need to do the same using C++ backend.
Thanks all for the kind reply.
--vineeth
...
Pelle Johnsen
14 years ago
Permalink
If your model is C++ based QAbstractListModel is the way to go.

You need to override the rowCount and data methods and then use
begin/endInsertRows when adding new items. This will inform ListView about
those changes and it will then update the appropriate delegates.

http://doc.qt.nokia.com/latest/qabstractlistmodel.html

<http://doc.qt.nokia.com/latest/qabstractlistmodel.html> -Pelle
...
vineeth
14 years ago
Permalink
Thanks a lot for the link on qabstractlistmodel , I could find the required
example here :
http://doc.trolltech.com/4.7.1/declarative-modelviews-abstractitemmodel.html
Also this is extremely efficient than earlier, where I used to repeatedly
reassign the model variable, every-time the data had changed.
--vineeth
...
Matti Mäki
14 years ago
Permalink
Check out this article from FN wiki:
http://wiki.forum.nokia.com/index.php/Using_QStandardItemModel_in_QML

I'm gladly using RoleItemModel in my application. It's really easy to
setup and use.

/matti
...
Loading...