Qt signal from model item to QML slot
-
Hey there. I've exhausted my googling and was hoping to get some direction from the forums.
I'm feeding a
ListViewwith a model derived fromQAbstractList. The items in this model areQObjects. Within theListView, I'd like to be able to have QML elements in the delegate listen to signals on their respectiveQObjects. Is this possible?What I've found suggests that every model item has to somehow be exposed using
setContextProperty, but that seems extremely tedious. Does the QML front-end not already know that the object in theListViewmodel has available signals?Thanks for any guidance.
An example of the QML. In this case, the color of the model item is being changed somewhere in the backend, and I'd like the QML to listen for the color change signals.
ListView { id: dataListView anchors.fill: parent model: itemModel delegate: ItemDelegate { Rectangle { id: itemColorIcon anchors.left: parent.left width: 20; height: 20 radius: width * 0.5 color: model.item.color Component.onCompleted: { // This is where I'd like to listen for signals on the model item model.item.color_changed.connect( function () { color = model.item.color; } ) } } Text { id: itemText Layout.fillWidth: true text: model.item.name elide: Text.ElideRight } } } -
In this case, the color of the model item is being changed somewhere in the backend
You don't require to do this. This is where Model View framework comes into picture. color change should automatically taken care by the model itself. I'm assuming that you have written custom model. Are you sending the dataChanged(..) signal whenever data is changed at model ?