Access modelData outside delegate



  • 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.



  • @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?
        }
    


  • @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)
        }
    


  • @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)
                }
            }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.