Can I specify the ListView's delegate text from outside the ListView?



  • I need to create a ListView with very specific formatting requirements in multiple places around my application. So it seems to make sense to create a Qml to define it:

    @MyListView.qml
    ListView
    {
    // interesting formatting goes here
    delegate: Text
    {
    text: itemNameInTheModel
    // more interesting formatting goes here
    }
    }@

    But then when I want to instantiate a MyListView, I can specify the model, but what if the model is outside my control and doesn't contain an item named "itemNameInTheModel"?

    I want to be able to say:

    @MyWindow.qml
    Rectangle
    {
    MyListView
    {
    id: accountList
    model: myData
    delegate.text: accountNumber
    }
    }@

    Is something like this possible?

    Thanks,
    Chris



  • You can try something like this. It may help you.

    @Rectangle {
    width: 200;height: 500;color :"red"
    property variant role1;
    property variant role2;
    Component{
    id : del
    Item {
    height: 50;width: 200
    Row {
    Text { text : role1}
    Text { text : role2}
    }
    }
    }

    ListView {
        id : view
        anchors.fill: parent
        model : MyModel {id :mymodel }
        delegate: del
    }
    Timer {
        id : tim
        interval: 5000
        running: true
        repeat: true
        property int count : 10
        onTriggered: {
            role1 = "cost1";
            role2 = "name1";
            mymodel.append({role1: count, role2:"Pizza"})
            count++;
        }
    }
    

    }

    ====MyModel.qml===
    ListModel {
    id :mod
    dynamicRoles: true
    }
    @


Log in to reply
 

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