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. Access modelData outside delegate
Forum Updated to NodeBB v4.3 + New Features

Access modelData outside delegate

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.1k 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.
  • E Offline
    E Offline
    Eligijus
    wrote on last edited by
    #1

    Hello,

    Here's example of my problem. I have component Test

    //Test.qml
    import QtQuick 2.6
    
    Item {
        id: base
        property var testText
        Column {
            Repeater {
                model: [1, 2, 3, 4, 5, 6]
    
                Text {
                    id: mText
                    text: base.testText
                }
            }
        }
    }
    

    And I want to do something like this

    // main.qml
    import QtQuick 2.6
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
    
        Test {
            anchors.fill: parent
            testText: modelData + 1 // Problem
        }
    }
    

    Basically I want to have reusable component where I could set delegate's property i.e. text and use modelData in there.

    E 1 Reply Last reply
    0
    • E Eligijus

      Hello,

      Here's example of my problem. I have component Test

      //Test.qml
      import QtQuick 2.6
      
      Item {
          id: base
          property var testText
          Column {
              Repeater {
                  model: [1, 2, 3, 4, 5, 6]
      
                  Text {
                      id: mText
                      text: base.testText
                  }
              }
          }
      }
      

      And I want to do something like this

      // main.qml
      import QtQuick 2.6
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 640
          height: 480
      
          Test {
              anchors.fill: parent
              testText: modelData + 1 // Problem
          }
      }
      

      Basically I want to have reusable component where I could set delegate's property i.e. text and use modelData in there.

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @Eligijus How about

      //Test.qml
      import QtQuick 2.6
      
      Item {
          id: base
          property var testText
          property var model
          Column {
              Repeater {
                  model: base.model
      
                  Text {
                      id: mText
                      text: base.testText + model.modelData
                  }
              }
          }
      }
      
      Test {
              anchors.fill: parent
              model: 5
              testText: "some text" // No Problem?
          }
      
      E 1 Reply Last reply
      0
      • E Eeli K

        @Eligijus How about

        //Test.qml
        import QtQuick 2.6
        
        Item {
            id: base
            property var testText
            property var model
            Column {
                Repeater {
                    model: base.model
        
                    Text {
                        id: mText
                        text: base.testText + model.modelData
                    }
                }
            }
        }
        
        Test {
                anchors.fill: parent
                model: 5
                testText: "some text" // No Problem?
            }
        
        E Offline
        E Offline
        Eligijus
        wrote on last edited by
        #3

        @Eeli-K Well solution only works for this particular example. What if I want to pass a function with modelData parameter?

        Test {
                anchors.fill: parent
                model: 5
                testText: function1(modelData)
            }
        
        Test {
                anchors.fill: parent
                model: 5
                testText: function2(modelData)
            }
        
        Test {
                anchors.fill: parent
                model: 5
                testText: "%1 meters".arg(modelData)
            }
        
        E 1 Reply Last reply
        0
        • E Eligijus

          @Eeli-K Well solution only works for this particular example. What if I want to pass a function with modelData parameter?

          Test {
                  anchors.fill: parent
                  model: 5
                  testText: function1(modelData)
              }
          
          Test {
                  anchors.fill: parent
                  model: 5
                  testText: function2(modelData)
              }
          
          Test {
                  anchors.fill: parent
                  model: 5
                  testText: "%1 meters".arg(modelData)
              }
          
          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @Eligijus model.modelData is available only for the delegate, not outside it. You have pass other things and parameters to the delegate and combine them there with the model data. Functions can also be passed as objects in javascript so you can do

          Test {
                  anchors.fill: parent
                  model: 5
                  functionObject: function2
              }
          
          Item {
              id: base
              property var functionObject
              Repeater {
                  ....
                  Text {
                          text: base.functionObject(model.modelData)
                      }
                  }
          
          1 Reply Last reply
          1

          • Login

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