Is it possible to create and delete the objects dynamically in a listmodel ?
-
wrote on 19 Jul 2017, 05:14 last edited by
I want to create a list model with dynamic objects in it. i.e. i want to add and delete the objects on particular event (eg: On clicking the mouse). Is it possible in qml to do it? if possible what are the constraints/problems that may occur in the application?
Thanks in advanceNitheesh.
-
I want to create a list model with dynamic objects in it. i.e. i want to add and delete the objects on particular event (eg: On clicking the mouse). Is it possible in qml to do it? if possible what are the constraints/problems that may occur in the application?
Thanks in advanceNitheesh.
wrote on 19 Jul 2017, 05:27 last edited by@Nitheesh Do you mean to say dynamic listmodel? Well, in that case you can,
- Build a model in C++ by inheriting QAbstractListModel and expose it to QML view.
- Make use of methods of ListModel in QML like,
append(jsobject dict)
object get(int index)
insert(int index, jsobject dict)
move(int from, int to, int n)
remove(int index, int count)
set(int index, jsobject dict)
-
wrote on 19 Jul 2017, 18:26 last edited by
Probably the easiest approach is to use a plain JavaScript array of plain JavaScript objects for your model. Then you can add/delete objects to the array however you like, using regular JavaScript methods.
-
wrote on 24 Jul 2017, 06:16 last edited by 6thC
Yes, it's very possible. If you are going to change your ListView model dynamically and want animations right, you'll want to use these 2x:
- listView.currentIndex
- listView.positionViewAtIndex()
I have maybe pushed it too far. I'm rebuilding my model much too frequently... when I turn my test data rate down to 1* 50m/s ... zooming ListView over thousands of items is a bit much to ask. Caching the data seems to be prudent, and if you don't do useless work it runs very nice. I'm on a very low end spec machine and I just can't believe the abuse I throw at it and it still feel smooth.
-
wrote on 24 Jul 2017, 09:12 last edited by
Hi,
This is an example of application i did.// LIST View
ListView{
snapMode: ListView.SnapToItem
clip: true
model: cptModel
delegate: QmiLabel {
height : w.height / 5 - (w.height / 5 * 0.05)
width:parent.width
text: lab + value
}}
// LIST Model
ListModel { id:cptModel property string label1 property string label2 /********************************************************************************/ property bool completed: false // list is completed Component.onCompleted: { /******************* Initialisation *******************/ cptModel.append({"lab":label1, value: 15}); cptModel.append({"lab":label2, value: /*any js expression*/}); completed = true; } }
LA
-
I want to create a list model with dynamic objects in it. i.e. i want to add and delete the objects on particular event (eg: On clicking the mouse). Is it possible in qml to do it? if possible what are the constraints/problems that may occur in the application?
Thanks in advanceNitheesh.
wrote on 25 Jul 2017, 04:07 last edited by@Nitheesh If using QAbstractModel use
remove(int index, int count = 1) which will remove the selected index.
If Qml Model use model_id.remove(index) -
Hi,
This is an example of application i did.// LIST View
ListView{
snapMode: ListView.SnapToItem
clip: true
model: cptModel
delegate: QmiLabel {
height : w.height / 5 - (w.height / 5 * 0.05)
width:parent.width
text: lab + value
}}
// LIST Model
ListModel { id:cptModel property string label1 property string label2 /********************************************************************************/ property bool completed: false // list is completed Component.onCompleted: { /******************* Initialisation *******************/ cptModel.append({"lab":label1, value: 15}); cptModel.append({"lab":label2, value: /*any js expression*/}); completed = true; } }
LA
wrote on 30 Aug 2017, 05:45 last edited by@LeLev Thanks a lot. this was exactly what i needed