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 do I append an item to my ListModel that's been defined not in the main.qml file

How do I append an item to my ListModel that's been defined not in the main.qml file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 551 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.
  • J Offline
    J Offline
    jimbolaya
    wrote on last edited by
    #1

    Let's say I have a ListModel component in A.qml:

    ListModel {
    id: alphabetModel
    }
    I have a seperate JS file to append items into my ListModel:

    alphabetModel.append({"Letter": "A"});
    If I run my code, I get a reference error:

    ReferenceError: alphabetModelis not defined
    I know I wouldn't get the ReferenceError if my ListModel was in main.qml and I did import my JS file into my A.qml.

    Any way of resolving this reference error?

    T 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Object of A is not created. Unless you create the the object like
      A{
      id : jim
      }

      Now you can all jim.append{}

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • J jimbolaya

        Let's say I have a ListModel component in A.qml:

        ListModel {
        id: alphabetModel
        }
        I have a seperate JS file to append items into my ListModel:

        alphabetModel.append({"Letter": "A"});
        If I run my code, I get a reference error:

        ReferenceError: alphabetModelis not defined
        I know I wouldn't get the ReferenceError if my ListModel was in main.qml and I did import my JS file into my A.qml.

        Any way of resolving this reference error?

        T Offline
        T Offline
        Tom_H
        wrote on last edited by
        #3

        @jimbolaya
        Try passing the object into a js method.

        function append(lst) {
          lst.append({"Letter": "A"})
        }
        
        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