Creating ListElements Dynamically Outside Parent Component



  • I have a component which i load

        Component{
            id:viewComp
            property alias compListMode: viewcompmodel
    
            Item{
                anchors.fill:parent
                id: mainItem
    
                ListView{
                    id:viewcomplist
                    model: viewcompmodel
                    delegate: categoryDelegate
                }
    
                ListModel{
                    id:viewcompmodel
                }
    
                Component {
                    id: categoryDelegate
                    Column {
                        width: 200
    
                        Rectangle {
                            id: categoryItem
                            border.color: "black"
                            border.width: 5
                            color: "white"
                            height: 50
                            width: 200
    
                            Text {
                                anchors.verticalCenter: parent.verticalCenter
                                x: 15
                                font.pixelSize: 24
                                text: Name
                            }
                        }
    
                    }
                }
            }
        }
    

    i need to dynamically create ListElements and SubElements for the "viewcompmodel" List Model outside of the component. How do i go about it ?

    I tried

    viewComp.compListMode.append(createListElement())
    
        function createListElement(elementname) {
            return {
                categoryName: elementname
            };
        }
    

    and i get the Error :

    Component objects cannot declare new properties
    

    and when this also give me fatal errors :

    
        function createListElement(elementname) {
            return {
                Name: "elementname"
               ListElement{
                     name: "Somenam"
                 }
            };
        }
    




  • This post is deleted!

Log in to reply
 

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