i know nothing
i know nothing

Reputation: 1002

Passing a parameter to a Component on instantiation

I'm creating TableViewColumns during runtime and need to pass the role to the TableViewColumn.

TableView {
    model: myModel

    onModelChanged: {
        var roleList = myModel.customRoleNames
        for ( var i = 0; i < roleList.length; ++i ) {
            var role = roleList[ i ]
            addColumn( /* my column component here */ )
        }
    }
}

Component {
    id: columnComponent

    TableViewColumn {
        role: /* ??? */
    }
}

How can I feed the role to my Component when it's being instantiated?

Upvotes: 1

Views: 162

Answers (1)

i know nothing
i know nothing

Reputation: 1002

Ok, I've got it. Here is the solution:

TableView {
    id: myTableView
    model: myModel

    onModelChanged: {
        var roleList = myModel.customRoleNames
        for ( var i = 0; i < roleList.length; ++i ) {
            var role = roleList[ i ]
            addColumn( columnComponent.createObject ( myTableView, { "role": role } ) )
        }
    }
}

Component {
    id: columnComponent

    TableViewColumn { }
}

This, of course, works for all properties of TableViewColumn, eg.:

addColumn( columnComponent.createObject ( myTableView, { "role": myRole, "title": someTitle } ) )

Upvotes: 1

Related Questions