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. ListView model: Javascript array?

ListView model: Javascript array?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 7.2k 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.
  • D Offline
    D Offline
    devDawg
    wrote on 6 Aug 2018, 18:30 last edited by
    #1

    Hi all,

    Simple and short question for today: Can I use a Javascript array as the model for a ListView?

    I have been trying to do this, and it doesn't appear to be possible. If someone does think it's possible, I will post my example code so you can take a look at how I'm trying to do it.

    Thanks!

    Anything worthwhile is never achieved easily.

    O 1 Reply Last reply 6 Aug 2018, 18:59
    0
    • D devDawg
      6 Aug 2018, 18:30

      Hi all,

      Simple and short question for today: Can I use a Javascript array as the model for a ListView?

      I have been trying to do this, and it doesn't appear to be possible. If someone does think it's possible, I will post my example code so you can take a look at how I'm trying to do it.

      Thanks!

      O Offline
      O Offline
      ODБOï
      wrote on 6 Aug 2018, 18:59 last edited by
      #2

      @devDawg hi,

      import "jsFile.js" as JsFile
      Window {
          width: 400
          height: 400
          visible: true
          ListView{
              height: parent.height
              width: parent.width/2
              model: JsFile.jsarr
              delegate:Text{
                  text:JsFile.jsarr[index]
                  height: 20
                  width: 50
              }
          }
      }
      

      / /jsFile.js

      var jsarr = [1,2,"egg",3,3.14]
      
      1 Reply Last reply
      0
      • D Offline
        D Offline
        devDawg
        wrote on 6 Aug 2018, 19:21 last edited by
        #3

        @LeLev Interesting. In my case, I don't have a .js file; I simply have a bunch of ListElements within a ListModel, where each ListElement has a "data" property that I've declared as an array using "[]". Each item in the data array is also a ListElement containing quite a bunch of properties, such as "name", "source", "type", etc. Here is some of my code to show you what I am talking about:

        ListView {
                        id: customGridList
                        width: 350
                        height: 480
                        anchors.top: parent.top
                        anchors.left: parent.left
                        spacing: 5
        
                        model: StreamModel { id: ref }
        
                        delegate: Component {
                            CustomGrid {
                                id: grid
                                width: 350
                                height: 480
                                
                                reference: ref.get(index)
                                
                            }
                        }
        

        StreamModel.qml:

        ListModel {
        
        id: streamQMLmodel
        
            ListElement {
        
                name: "Operator HMI"
                type: "module"
                data_size: 16
                p1_size: 3
                comm_size: 0
        
                data: [ ListElement { sourceID: -1; source: "-"; isAnalog: true; name: "Key"; type: "enum"; value: "0"; units: "-"; priority: "1";
                 minimum: "0"; maximum: "2"; warnMaximumLevel: "-"; warnMinimumLevel: "-"; critMaximumLevel: "-"; critMinimumLevel: "-"; moduleName: "Operator HMI"; },
        
                 ListElement { sourceID: -1; source: "-"; isAnalog: true; name: "Mode"; type: "enum"; value: "0"; units: "-"; minimum: "0";
                 maximum: "3"; priority: "2"; warnMaximumLevel: "-"; warnMinimumLevel: "-"; critMaximumLevel: "-"; critMinimumLevel: "-"; moduleName: "Operator HMI"; },
        
                 ListElement{ sourceID: -1; source: "-"; isAnalog: true; name: "Transmission Shift"; type: "enum"; value: "0"; units: "-"; minimum: "0";
                 maximum: "3";priority: "1"; warnMaximumLevel: "-"; warnMinimumLevel: "-"; critMaximumLevel: "-"; critMinimumLevel: "-"; moduleName: "Operator HMI"; }, ...
        
        and on and on.
        
        

        CustomGrid.qml is where I try to pass my array as a model:

        ListView {
            id: root
            property var reference //Pass in the indexed ListElement. we then use the Repeater on it's data property.
        
            model: reference.data
        
            delegate: Component {
                GridRowDelegate {
                    height: 18
        
                    cell1str: model.get(index).name
                    cell2str: reference.data[index].name
                    cell3str: "jimmy"
                }
            }
        

        for the assignment of cell1str, I get Property 'get' of object QQmlDMAbstractItemModelData(0x108cf00) is not a function.

        for the assignment of cell2str, I get TypeError: Cannot read property 'name' of undefined.

        So the data object is coming in as undefined, even though it's a property of reference..

        Anything worthwhile is never achieved easily.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 6 Aug 2018, 21:37 last edited by
          #4

          ListView and other views are totally compatible with js arrays.

          You don't even have to access the array by indexing it, the view does that for you.

          However, I guess you should not put ListElement in your array, just js objects.so data: [ ListElement { sourceID: -1; ... should be data: [ { sourceID: -1; ...

          How are you passing the indexed ListElement in your reference property ?

          Anyway, you should access it in your GridViewDelegate like so: cell1str: modelData.name.
          modelData is how you can access data from a dumb model with no roles.

          D 1 Reply Last reply 7 Aug 2018, 19:27
          2
          • G GrecKo
            6 Aug 2018, 21:37

            ListView and other views are totally compatible with js arrays.

            You don't even have to access the array by indexing it, the view does that for you.

            However, I guess you should not put ListElement in your array, just js objects.so data: [ ListElement { sourceID: -1; ... should be data: [ { sourceID: -1; ...

            How are you passing the indexed ListElement in your reference property ?

            Anyway, you should access it in your GridViewDelegate like so: cell1str: modelData.name.
            modelData is how you can access data from a dumb model with no roles.

            D Offline
            D Offline
            devDawg
            wrote on 7 Aug 2018, 19:27 last edited by
            #5

            @GrecKo Hi! Thanks for the reply. I got this to work, but not using traditional JS array functions.

            For example, let's say the ListElement I am interested in contains a property called "outputs", which is an array []. If this ListElement was contained inside of a model, I would access elements of outputs like this:

            model.get(index).outputs.get(output_index);
            

            This works great! Now on to the next task..

            Thanks for the feedback, guys!

            Anything worthwhile is never achieved easily.

            1 Reply Last reply
            0

            1/5

            6 Aug 2018, 18:30

            • Login

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