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. Trying to store javascript array as a QML property
QtWS25 Last Chance

Trying to store javascript array as a QML property

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 10 Posters 36.1k 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.
  • P Offline
    P Offline
    phaikawl
    wrote on last edited by
    #1

    For example, I have this QML object:
    @Item {
    id: something
    }@

    I just wanna store a javascript array "abcArray" as QML property, just to make it usable in other functions and other QML objects.
    So I tried doing this

    @Item {
    id: something
    property variant abcArray: Array()

    //A demo function
    function demo() {
    abcArray.push("I want to push this!!!");
    abcArray.push("Please, please appear in the array!");
    abcArray.push("crazy!");
    //print out the length of the array
    console.log(abcArray.length);
    //Oh damn! it just outputs "0" all the time even if I pushed many things to it
    }
    }@

    In the end, it doesn't work, after all, the abcArray is still completely empty.
    How to make it work?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leafaku
      wrote on last edited by
      #2

      You can use ListModel to do that.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phaikawl
        wrote on last edited by
        #3

        [quote author="leafaku" date="1331299888"]You can use ListModel to do that.[/quote]
        But it would be too much for this simple thing, and it's a waste of memory! I just need to have an array of strings and no more. I don't need the "dict" thing of ListModel.

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          You can use a javascript function that returns an array in the property

          //in js file:
          var arr{}

          Function Array() {
          ...
          Return arr
          }

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dajansen
            wrote on last edited by
            #5

            @Item {
            id: something
            property variant abcArray

            Component.onCompleted: {
            abcArray = demo();
            console.log(abcArray.length,abcArray);
            }
            //A demo function
            function demo() {
            var tmp = []
            tmp.push("I want to push this!!!");
            tmp.push("Please, please appear in the array!");
            tmp.push("crazy!");
            return tmp;
            }
            }@

            QtQuick Quality Engineer / Lab Monkey
            Nokia Brisbane

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Timmmm
              wrote on last edited by
              #6

              I spend ages trying to work this out and finally discovered what none of the other threads say: there is now a "var" property which can contain any javascript object. For example:

              property var myArray = []

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lfxciii
                wrote on last edited by
                #7

                thank you Damian

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  chrisadams
                  wrote on last edited by
                  #8

                  There are docs, you know ;-) Although I agree that they've been made hideously difficult to navigate now, since 5.0 times, for some reason.

                  See
                  http://doc-snapshot.qt-project.org/qt5-stable/qmlreference.html
                  and
                  http://doc-snapshot.qt-project.org/qt5-stable/qtqml-typesystem-topic.html
                  for more information.

                  Cheers,
                  Chris.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Traxx
                    wrote on last edited by
                    #9

                    Thanks all.This definitely useful tips.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AlexMerkulov
                      wrote on last edited by
                      #10

                      404 - Page Not Found.

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        xargs1
                        wrote on last edited by
                        #11

                        http://doc.qt.io/qt-5/qtqml-typesystem-topic.html

                        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