Important: Please read the Qt Code of Conduct -

Is it possible to create and delete the objects dynamically in a listmodel ?

  • 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 advance


  • @Nitheesh Do you mean to say dynamic listmodel? Well, in that case you can,

    1. Build a model in C++ by inheriting QAbstractListModel and expose it to QML view.
    2. 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)

  • 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.

  • 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:

    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.

  • Hi,
    This is an example of application i did.

    // LIST View
    snapMode: ListView.SnapToItem
    clip: true
    model: cptModel
    delegate: QmiLabel {
    height : w.height / 5 - (w.height / 5 * 0.05)
    text: lab + value


    // LIST Model

    ListModel {
        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;


  • @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)

  • @LeLev Thanks a lot. this was exactly what i needed

Log in to reply