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. How does the Append method add attributes to the ListModel?
Qt 6.11 is out! See what's new in the release blog

How does the Append method add attributes to the ListModel?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 662 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by mirro
    #1
    fruitModel.append({ "name": "Apple", "description": "Core"})
    
    ListModel {
        id: fruitModel
    
        ListElement {
            name: "Apple"
            attributes: [
                ListElement { description: "Core" },
            ]
        }
    }
    
    
    1 Reply Last reply
    0
    • M mirro

      @Allon

      https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html
      Methods
      append(dict)

      clear()
      object get(index)
      insert(index, jsobject dict)
      move(from, int to, int n)
      remove(index, int count = 1)
      set(index, jsobject dict)
      setProperty(index, string property, variant value)
      sync()

      A Offline
      A Offline
      Allon
      wrote on last edited by
      #6

      @mirro Sorry read the documentation too quickly.

      To answer your question, you forgot the " around attributes and you forgot the : to tell that the first word is a property, then you forgot the brackets to tell that it is an array.
      { "name": "Apple", "attributes": [{ description: "table" }]}
      instead of
      {"name": "Apple", attributes{"description": "Core"}

      Here is a code to illustrate it:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 2.12
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
      
          ListModel {
          id: fruitModel
      
              ListElement {
                  name: "Apple"
                  attributes: [
                      ListElement { description: "Core" }
                  ]
              }
          }
      
          Button {
              id: btn
              onClicked: {
                  fruitModel.append({ "name": "Apple", "attributes": [{ description: "table" }]})
                  var keysList = JSON.stringify(fruitModel)
                     console.log(keysList)
      
      
                  console.log(JSON.stringify(fruitModel))
              }
          }
      
          ListView {
              width: 180; height: 200
      
              anchors.top: btn.bottom
      
              model: fruitModel
              delegate: Text {
                  text: name + ": " + attributes.get(0).description
              }
          }
      }
      

      Regards,

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Allon
        wrote on last edited by
        #2

        What is exactly your question?
        Regards,

        M 1 Reply Last reply
        0
        • A Allon

          What is exactly your question?
          Regards,

          M Offline
          M Offline
          mirro
          wrote on last edited by
          #3

          @Allon What is wrong with the following QML code syntax?

          fruitModel.append({ "name": "Apple", attributes{"description": "Core"}})
          ListModel {
          id: fruitModel

          ListElement {
              name: "Apple"
              attributes: [
                  ListElement { description: "Core" },
              ]
          }
          

          }

          A 1 Reply Last reply
          0
          • M mirro

            @Allon What is wrong with the following QML code syntax?

            fruitModel.append({ "name": "Apple", attributes{"description": "Core"}})
            ListModel {
            id: fruitModel

            ListElement {
                name: "Apple"
                attributes: [
                    ListElement { description: "Core" },
                ]
            }
            

            }

            A Offline
            A Offline
            Allon
            wrote on last edited by
            #4

            There is no append() function for ListModel

            https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html

            M 1 Reply Last reply
            0
            • A Allon

              There is no append() function for ListModel

              https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html

              M Offline
              M Offline
              mirro
              wrote on last edited by
              #5

              @Allon

              https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html
              Methods
              append(dict)

              clear()
              object get(index)
              insert(index, jsobject dict)
              move(from, int to, int n)
              remove(index, int count = 1)
              set(index, jsobject dict)
              setProperty(index, string property, variant value)
              sync()

              A 1 Reply Last reply
              0
              • M mirro

                @Allon

                https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html
                Methods
                append(dict)

                clear()
                object get(index)
                insert(index, jsobject dict)
                move(from, int to, int n)
                remove(index, int count = 1)
                set(index, jsobject dict)
                setProperty(index, string property, variant value)
                sync()

                A Offline
                A Offline
                Allon
                wrote on last edited by
                #6

                @mirro Sorry read the documentation too quickly.

                To answer your question, you forgot the " around attributes and you forgot the : to tell that the first word is a property, then you forgot the brackets to tell that it is an array.
                { "name": "Apple", "attributes": [{ description: "table" }]}
                instead of
                {"name": "Apple", attributes{"description": "Core"}

                Here is a code to illustrate it:

                import QtQuick 2.12
                import QtQuick.Window 2.12
                import QtQuick.Controls 2.12
                
                Window {
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Hello World")
                
                
                    ListModel {
                    id: fruitModel
                
                        ListElement {
                            name: "Apple"
                            attributes: [
                                ListElement { description: "Core" }
                            ]
                        }
                    }
                
                    Button {
                        id: btn
                        onClicked: {
                            fruitModel.append({ "name": "Apple", "attributes": [{ description: "table" }]})
                            var keysList = JSON.stringify(fruitModel)
                               console.log(keysList)
                
                
                            console.log(JSON.stringify(fruitModel))
                        }
                    }
                
                    ListView {
                        width: 180; height: 200
                
                        anchors.top: btn.bottom
                
                        model: fruitModel
                        delegate: Text {
                            text: name + ": " + attributes.get(0).description
                        }
                    }
                }
                

                Regards,

                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