Accessing ListItemComponents on the run

2019-07-25 00:26发布

问题:

right now I'm trying to create a ListView which loads the dataModel using a custom QML. Here's the snippet of my code:

ListView {
            id: firstPageListView
            visible: false
            dataModel: firstPageDataModel

            layout: GridListLayout {
                columnCount: 1
                cellAspectRatio: 2.0
                headerMode: ListHeaderMode.Standard
                verticalCellSpacing: 10
            }

            listItemComponents: [

                ListItemComponent {
                    //custom qml that will be used
                    ThumbNote {
                        title: ListItemData.title
                        text: ListItemData.text
                        imageSource: ListItemData.image
                        listmode: true //list mode
                        date: ListItemData.date

                    }
                }
            ]

        }

I want to create a button that will change the listmode property of each component into false. By doing so, the object will invoke a function that set in the onListModeChanged() of the ThumbNote QML.

Sorry for my poor english, any help would be appreciated. :)

回答1:

Perhaps you might consider adding a property to the ListView and binding the ThumbNotes' properties to it.

E.g.:

ListView {
        id: firstPageListView
        visible: true
        dataModel: firstPageDataModel

        property bool listMode: true
        ...
        listItemComponents: [

            ListItemComponent {
                //custom qml that will be used
                ThumbNote {
                    title: ListItemData.title
                    text: ListItemData.text
                    imageSource: ListItemData.image
                    listmode: firstPageListView.listMode
                    date: ListItemData.date

                }
            }
        ]

}
Button {
    onClicked: {
        firstPageListView.listMode = false; 
    }
}