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. Define ListElement at an external file
Qt 6.11 is out! See what's new in the release blog

Define ListElement at an external file

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.3k 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.
  • _ Offline
    _ Offline
    _dmp
    wrote on last edited by
    #1

    hello

    I want define the ListElement in different file and use code like this:

    MyListModel.qml
    @import QtQuick 2.0

    ListModel {
    dynamicRoles: true

    ListElement {
        someitems:[
            SomeItem{},
            SomeItem{},
            SomeItem{},
            SomeItem{}
        ]
    }
    

    }@

    SomeItem.qml
    @import QtQuick 2.0

    ListElement {
    recordID: Math.round(Math.random()*1000)
    eqType: 0
    message: "some text"
    }@

    main.qml
    @import QtQuick 2.0

    Item {
    anchors.fill: parent
    property MyListModel myListModel: MyListModel{}

        ListView {
                width: parent.width
                height: parent.height
    
                model: myListModel.get(0).someitems
                delegate:
                    Rectangle {
                        width: parent.width
                        height: 90
                    }
        }
    

    }@

    i got runtime error at file MyListModel.qml at line 8

    ListElement : cannot contain nested elements

    how resolve it?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vincent007
      wrote on last edited by
      #2

      @
      import QtQuick 2.0

      ListModel {
          dynamicRoles: true
       
          ListElement {
              someitems:[
                  ListElement{
                        recordID: Math.round(Math.random()*1000)
                        eqType: 0
                        message: "some text"
                  },
                  ListElement{
                        recordID: Math.round(Math.random()*1000)
                        eqType: 0
                        message: "some text"
                  },
                  ListElement{
                        recordID: Math.round(Math.random()*1000)
                        eqType: 0
                        message: "some text"
                  }
              ]
          }
      }
      

      @

      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