Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is it possible to create and delete the objects dynamically in a listmodel ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 6 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nitheesh
    wrote on 19 Jul 2017, 05:14 last edited by
    #1

    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.

    Y A 2 Replies Last reply 19 Jul 2017, 05:27
    0
    • N Nitheesh
      19 Jul 2017, 05:14

      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.

      Y Offline
      Y Offline
      Yashpal
      wrote on 19 Jul 2017, 05:27 last edited by
      #2

      @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)
      1 Reply Last reply
      2
      • W Offline
        W Offline
        wpurvis
        wrote on 19 Jul 2017, 18:26 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • 6 Offline
          6 Offline
          6thC
          wrote on 24 Jul 2017, 06:16 last edited by 6thC
          #4

          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.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            ODБOï
            wrote on 24 Jul 2017, 09:12 last edited by
            #5

            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

            N 1 Reply Last reply 30 Aug 2017, 05:45
            2
            • N Nitheesh
              19 Jul 2017, 05:14

              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.

              A Offline
              A Offline
              Arvindhan Ponnusamy
              wrote on 25 Jul 2017, 04:07 last edited by
              #6

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

              1 Reply Last reply
              2
              • O ODБOï
                24 Jul 2017, 09:12

                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

                N Offline
                N Offline
                Nitheesh
                wrote on 30 Aug 2017, 05:45 last edited by
                #7

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

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved