[SOLVED] How to add item to ListView?
Page |
1 |
I have ListView and I want to add new items by timer (for example).
I didn’t understand how…
I found only String ListModel Example [doc.qt.nokia.com] and AbstractItemModel Example [doc.qt.nokia.com]. But it’s static filling, not dynamic.
Example: every n second add new item to ListView with current time. How can I do this?
Thanks.
17 replies
Use of QStandardItemModel comes into mind.
just call
- // populate the model here, if you want
- listView->setModel(stdModel);
QStandardItemModel is a subclass of QAbstractItemModel, so you can use it for every item view (not for the item widgets, those do not need a model!). The QStandardItemModel has a basic usage example too.
If you want to add new entries to the model, just call the respective methods of QStandardItemModel, like appendRow, insertRow, etc.
You can always use insert, append, clear methods of QML ListModel Element [doc.qt.nokia.com].
Hmm, If you exposed your QAbstractItemModel derived model to QML like this
- QDeclarativeView* iView;
- //...
- //MyListModel derived from QAbstractItemModel
- MyListModel* iListModel;
- //...
- iView->rootContext()->setContextProperty("listModel",iListModel);
and set it in qml list like this
- ListView {
- //...
- model:listModel
- //...
- }
then every time you insert something to your model you have to call
- endInsertRows();
and your QML ListView will be updated
You will have to supply slots on your model (or another class that you have also exposed to QML) that provide this functionality. However, because you are using a QAbstractItemModel, we have no idea what kind of model that is, so it is impossible to help you with the details. Are you sure you need to have your model in C++ though? I mean, if you just want to append simple texts, why not use one of the models QML supplies you?
You must log in to post a reply. Not a member yet? Register here!


