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 ListView with a model derived from QAbstractList. The items in this model are QObjects. Within the ListView, I'd like to be able to have QML elements in the delegate listen to signals on their respective QObjects. 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 the ListView model 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
            }
        }
    }
    


  • This post is deleted!

Log in to reply
 

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