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. Can't access ListModel's roles
Forum Updated to NodeBB v4.3 + New Features

Can't access ListModel's roles

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 396 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.
  • pillexyz_zP Offline
    pillexyz_zP Offline
    pillexyz_z
    wrote on last edited by
    #1

    Hi! I'm still a beginner and trying to figure out why I get error "ReferenceError" model is not defined.

    The Repeater documentation says: If the model is a model object (such as a ListModel) the delegate can access all model roles as named properties, in the same way that delegates do for view classes like ListView.

    And here is my code I'm trying to do that but it doesn't work for some reason. It works if I access it like this: macroItemModel.get(index).macroCode but this sounds more complicated than what the documentation says.

    (sorry for the pastebin it marked the post as spam with the code put it in here, between the code tags)
    https://pastebin.com/grd7m3hA

    (Also there is another problem when I remove an element from ListModel I get 2 more TypeErrors: Cannot read property 'macroName', 'macroCode' of undefined)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SnehaSuresh
      wrote on last edited by SnehaSuresh
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        The issue is that accessing roles in a delegate through context properties is not possible anymore when you define a required property.
        If you change the required property int index in property int index: model.index your delegate will work.
        That's not the preferred solution however, you should use required properties for everything.

        So keep your required index, and make macroName and macroCode required. You can make them required in your delegate even if they are already defined in MacroItem by writing required <propertyName>:

        delegate: MacroItem {
            required property int index
            containerWidth: macroListContainer.width/1.5
            required macroName
            required macroCode
            onDeleteButtonClicked: {
                macroItemModel.remove(index, 1);
            }
        }
        
        pillexyz_zP 1 Reply Last reply
        1
        • GrecKoG GrecKo

          The issue is that accessing roles in a delegate through context properties is not possible anymore when you define a required property.
          If you change the required property int index in property int index: model.index your delegate will work.
          That's not the preferred solution however, you should use required properties for everything.

          So keep your required index, and make macroName and macroCode required. You can make them required in your delegate even if they are already defined in MacroItem by writing required <propertyName>:

          delegate: MacroItem {
              required property int index
              containerWidth: macroListContainer.width/1.5
              required macroName
              required macroCode
              onDeleteButtonClicked: {
                  macroItemModel.remove(index, 1);
              }
          }
          
          pillexyz_zP Offline
          pillexyz_zP Offline
          pillexyz_z
          wrote on last edited by
          #4

          @GrecKo Thanks for your answer, now it finally works. ^_^

          1 Reply Last reply
          0
          • pillexyz_zP pillexyz_z has marked this topic as solved on

          • Login

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