Adding items to JS list which is used as a model
-
wrote on 12 Oct 2018, 15:02 last edited by maydin 10 Dec 2018, 15:03
I have a ListView and its model is a JavaScript list. For example if I declare it as
property var options: [{text: "ttt"}]
and use it in delegate as modelData.text there is no problem in first initialization.But when i want to add an item to that list using a button etc. it doesnt do anything. I call options.push(item) but neither it gives an error nor updates view.
I may use QML item models but this is easier since i get this input from a JSON source.
-
wrote on 12 Oct 2018, 16:28 last edited by
Hi,
this can help for the data persistence : https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodelyou can parse your json file first, then you create a ListModel with the data (methode in the first link)
then you add data using append() http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method
and save on exit
-
Hi,
this can help for the data persistence : https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodelyou can parse your json file first, then you create a ListModel with the data (methode in the first link)
then you add data using append() http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method
and save on exit
wrote on 13 Oct 2018, 07:06 last edited by@LeLev I know how to do it like in link you gave. He uses for loop to add them to a ListModel:
for (var i = 0; i < datamodel.length; ++i) dataModel.append(datamodel[i])
But I can use a simple JavaScript list as a model and i want to push items to that directly. But model doesnt update when use list.push().
If i cannot find a solution i have to use that ListModel way ofcourse.
-
wrote on 15 Oct 2018, 03:37 last edited by
@maydin said in Adding items to JS list which is used as a model:
property var options:
Declare as
property var options:["ttt"]
To add items use
options.push("Some Text")
-
@maydin said in Adding items to JS list which is used as a model:
property var options:
Declare as
property var options:["ttt"]
To add items use
options.push("Some Text")
-
I have a ListView and its model is a JavaScript list. For example if I declare it as
property var options: [{text: "ttt"}]
and use it in delegate as modelData.text there is no problem in first initialization.But when i want to add an item to that list using a button etc. it doesnt do anything. I call options.push(item) but neither it gives an error nor updates view.
I may use QML item models but this is easier since i get this input from a JSON source.
wrote on 15 Oct 2018, 11:10 last edited by@maydin Pure Javascript array could not be models easily, instead, use
ListModel
, or use C++QStringList
or other C++ data structure. -
wrote on 15 Oct 2018, 12:11 last edited by
If you dont need to change model, then using JS models is easiest solution. But if you can change model then you have to use ListModel or C++ Model.
I think we reached the final conclusion. But not sure should I mark as solved.
1/8