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. Adding items to JS list which is used as a model

Adding items to JS list which is used as a model

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 4 Posters 2.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.
  • M Offline
    M Offline
    maydin
    wrote on last edited by maydin
    #1

    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.

    jiancaiyangJ 1 Reply Last reply
    0
    • ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      Hi,
      this can help for the data persistence : https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodel

      you 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

      M 1 Reply Last reply
      0
      • ODБOïO ODБOï

        Hi,
        this can help for the data persistence : https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodel

        you 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

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

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

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          Yaswanth
          wrote on last edited by
          #4

          @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")
          
          M 1 Reply Last reply
          0
          • Y Yaswanth

            @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")
            
            M Offline
            M Offline
            maydin
            wrote on last edited by
            #5

            @Yaswanth Read my first post. I did same but it doesnt update view.

            Y 1 Reply Last reply
            0
            • M maydin

              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.

              jiancaiyangJ Offline
              jiancaiyangJ Offline
              jiancaiyang
              wrote on last edited by
              #6

              @maydin Pure Javascript array could not be models easily, instead, use ListModel, or use C++ QStringList or other C++ data structure.

              我们自己的论坛:http://qtdream.com
              擅长三维角色仿真动画。

              1 Reply Last reply
              0
              • M maydin

                @Yaswanth Read my first post. I did same but it doesnt update view.

                Y Offline
                Y Offline
                Yaswanth
                wrote on last edited by Yaswanth
                #7

                @maydin I checked it again, Lisview is not updating after adding elements. But if you reassign the model to the listview, it gets updated.
                One more way is using ListModel in qml.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maydin
                  wrote on last edited by
                  #8

                  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 Reply Last reply
                  0

                  • Login

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