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. component loaded with Loader inside a delegate, how to access model?
Forum Update on Monday, May 27th 2025

component loaded with Loader inside a delegate, how to access model?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 10.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.
  • L Offline
    L Offline
    lqsa
    wrote on 24 Jun 2016, 19:12 last edited by lqsa
    #1

    Inside a delegate that has a Loader, how the component loaded by Loader can access the model?

    ComponentBase.qml:

    Item {
       ...
       property Component insideDelegateComponent
       ...
       ListView {
          ...
          delegate: Item {
              ...
              Loader {
                  sourceComponent: insideDelegateComponent
              }
              ... 
          }
       }
       ...
    }
    

    ComponentPaintDelegate.qml

    Text {
        text: model.name
    }
    

    DrawView.qml

    ComponentBase {
       insideDelegateComponent : ComponentPaintDelegate {
          ...
       }
    }
    

    Gives the error: Cannot read property 'name' of undefined

    S 1 Reply Last reply 29 Jun 2016, 09:07
    0
    • L lqsa
      24 Jun 2016, 19:12

      Inside a delegate that has a Loader, how the component loaded by Loader can access the model?

      ComponentBase.qml:

      Item {
         ...
         property Component insideDelegateComponent
         ...
         ListView {
            ...
            delegate: Item {
                ...
                Loader {
                    sourceComponent: insideDelegateComponent
                }
                ... 
            }
         }
         ...
      }
      

      ComponentPaintDelegate.qml

      Text {
          text: model.name
      }
      

      DrawView.qml

      ComponentBase {
         insideDelegateComponent : ComponentPaintDelegate {
            ...
         }
      }
      

      Gives the error: Cannot read property 'name' of undefined

      S Offline
      S Offline
      sylc
      wrote on 29 Jun 2016, 09:07 last edited by
      #2

      @lqsa Loader has a signal 'loaded' so you could make a signal handler:

      Loader {
         ...
         onLoaded: {
            item.role1 = role1;
            item.role2 = role2;
         }
      }
      

      Obviously the component that you load must have these properties roleX.

      L 1 Reply Last reply 29 Jun 2016, 12:25
      0
      • S sylc
        29 Jun 2016, 09:07

        @lqsa Loader has a signal 'loaded' so you could make a signal handler:

        Loader {
           ...
           onLoaded: {
              item.role1 = role1;
              item.role2 = role2;
           }
        }
        

        Obviously the component that you load must have these properties roleX.

        L Offline
        L Offline
        lqsa
        wrote on 29 Jun 2016, 12:25 last edited by
        #3

        @sylc It doesn't work with the model. I suppose that the problem is that the model is attached from the context when the component is created and created bindings of their properties.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sylc
          wrote on 29 Jun 2016, 16:39 last edited by
          #4

          Well, the problem is the model's roles aren't visible from the loader inside the delegate. Here's a code snippet where I've shown properties trick.

          Does it fix the issue?

          import QtQuick 2.7
          
          Item {
              width: 640
              height: 480
          
              ListModel {
                  id: peopleModel
                  ListElement { name: "Samantha"; age: "21" }
                  ListElement { name: "Joe"; age: "33" }
                  ListElement { name: "Alex"; age: "32" }
              }
          
          
              Column {
                  anchors.fill: parent
                  Repeater {
                      anchors.fill: parent
          
                      model: peopleModel
          
                      Loader {
                          sourceComponent: aDelegate
                          onLoaded: { item.name = name; item.age = age; }
                      }
                  }
              }
          
              Component {
                  id: aDelegate
                  Text {
                      property string name
                      property int age
                      x: 5
                      height: 15
                      text: name + "," + age
                  }
              }
          }
          
          1 Reply Last reply
          0
          • jpnurmiJ Offline
            jpnurmiJ Offline
            jpnurmi
            wrote on 29 Jun 2016, 17:19 last edited by jpnurmi
            #5

            Loader creates an extra context for the dynamically instantiated item. Consequently, if I remember correctly, a property defined in the Loader element itself is visible to the loaded item. The styling of Qt Quick Controls 1.x is based on this technique.

            Loader {
                property string modelName: model.name
                sourceComponent: exampleComponent
            }
            
            Component {
                id: exampleComponent
                Text {
                    text: modelName
                }
            }
            
            S 1 Reply Last reply 29 Jun 2016, 18:47
            0
            • jpnurmiJ jpnurmi
              29 Jun 2016, 17:19

              Loader creates an extra context for the dynamically instantiated item. Consequently, if I remember correctly, a property defined in the Loader element itself is visible to the loaded item. The styling of Qt Quick Controls 1.x is based on this technique.

              Loader {
                  property string modelName: model.name
                  sourceComponent: exampleComponent
              }
              
              Component {
                  id: exampleComponent
                  Text {
                      text: modelName
                  }
              }
              
              S Offline
              S Offline
              sylc
              wrote on 29 Jun 2016, 18:47 last edited by
              #6

              @jpnurmi Yes, you are right. That's even better!

              import QtQuick 2.7
              
              Item {
                  width: 640
                  height: 480
              
                  ListModel {
                      id: peopleModel
                      ListElement { name: "Samantha"; age: "21" }
                      ListElement { name: "Joe"; age: "33" }
                      ListElement { name: "Alex"; age: "32" }
                  }
              
                  Column {
                      anchors.fill: parent
                      Repeater {
                          anchors.fill: parent
              
                          model: peopleModel
              
                          Loader {
                              sourceComponent: aDelegate
                              property string modelName: name
                              property int modelAge: age
                          }
                      }
                  }
              
                  Component {
                      id: aDelegate
                      Text {
                          x: 5
                          height: 15
                          text: modelName + "," + modelAge
                      }
                  }
              }
              
              1 Reply Last reply
              0
              • L Offline
                L Offline
                lqsa
                wrote on 29 Jun 2016, 19:31 last edited by
                #7

                It works!

                Only remarks that the component delegated can't has model.<property>, it must be only <property> and the Loader must have a property for every model property, not a model property.

                Thank you very much!

                jpnurmiJ 1 Reply Last reply 29 Jun 2016, 21:04
                0
                • L lqsa
                  29 Jun 2016, 19:31

                  It works!

                  Only remarks that the component delegated can't has model.<property>, it must be only <property> and the Loader must have a property for every model property, not a model property.

                  Thank you very much!

                  jpnurmiJ Offline
                  jpnurmiJ Offline
                  jpnurmi
                  wrote on 29 Jun 2016, 21:04 last edited by
                  #8

                  With a little bit of trickery, using the same technique you should be able to expose 'model' as is to the loaded item:

                  delegate: Item {
                      property var itemModel: model // read 'model' from the delegate's context
                      width: loader.width
                      height: loader.height
                  
                      Loader {
                          id: loader
                          property var model: parent.itemModel // inject 'model' to the loaded item's context
                          sourceComponent: exampleComponent
                      }
                  }
                  
                  Component {
                      id: exampleComponent
                      Text {
                          text: model.name
                      }
                  }
                  
                  1 Reply Last reply
                  2

                  1/8

                  24 Jun 2016, 19:12

                  • Login

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