Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Update Listview elements on signal in qml



  • Hi,
    I am using listview in order to display vertical list of elements. I want to update the listview elements (Text property) on the reception of signal in qml.
    How could i update the listview elements'property using c++ functions on signal received...?

    Below is the snippet of my code:

        Component {
            id: my_delegate
    
                Item {
                    id: alarm_1
                    width: 680
                    height: 190
    
                    Rectangle {
                        x:0
                        y:188
                        width:(680-10)
                        height:4
                        smooth: false
                        border.width: 2
                        border.color: "#303030"
                    }
    
                    Text {
                        id: alarm_primary_zone
                        x: 20
                        y: 20
                        width: 300
                        height: 26
                        color: "#e0e0e0"
                        text: alarm.getAlarmPrimZone(index)
                        font.family: "Honeywell Sans"
                        wrapMode: Text.WordWrap
                        verticalAlignment: Text.AlignTop
                        font.pixelSize: 22
                        horizontalAlignment: Text.AlignLeft
                    }
    
                    Text {
                        id: alarm_1_primary_zone_label
                        x: 20
                        y: 54
                        width: 300
                        height: 26
                        color: "#e0e0e0"
                        text:alarm.getAlarmZoneLabel(index)
                        font.family: "Honeywell Sans"
                        wrapMode: Text.WordWrap
                        verticalAlignment: Text.AlignTop
                        font.pixelSize: 22
                        horizontalAlignment: Text.AlignLeft
                    }
    
                    Text {
                        id: alarm_1_device_label
                        x: 20
                        y: 88
                        width: 300
                        height: 26
                        color: "#e0e0e0"
                        text: alarm.getAlarmPointInfo(index)
                        font.family: "Honeywell Sans"
                        wrapMode: Text.WordWrap
                        verticalAlignment: Text.AlignTop
                        font.pixelSize: 22
                        horizontalAlignment: Text.AlignLeft
                    }
    
                    Text {
                        id: alarm_1_date_time_stamp
                        x: 450
                        y: 88
                        width: 211
                        height: 26
                        color: "#ffffff"
                        text: alarm.getAlarmTimestamp(index)
                        font.family: "Honeywell Sans"
                        wrapMode: Text.WordWrap
                        verticalAlignment: Text.AlignTop
                        font.pixelSize: 22
                        horizontalAlignment: Text.AlignLeft
                    }
    }
    
        ListView {
            id:view
            x:120
            y:80
            width:680
            height:320
            contentWidth: 680
            delegate: my_delegate
            model: alarm.getDisplayEvtCount(); //no of elements in list
            boundsBehavior: Flickable.StopAtBounds
            flickableDirection:Flickable.VerticalFlick
            orientation: Qt.Vertical
            spacing: 0
            interactive: true
            cacheBuffer: 190
        }
    Connections {
             onDisplayListUpdate {
                      //  want to update listview elements here 
                      //  e.g alarm_primary_zone.text = "New Text"  or call c++ function alarm.getAlarmPrimZone(index)
             }
    }
    

  • Moderators

    You're doing it backwards. You should update the model used by ListView, then the delegates will update automatically.

    Currently you're just using an integer model, so easiest way to trigger an update is to do:

    view.model = 0
    view.model = alarm.getDisplayEvtCount();
    

    But a much better and future-proof solution is to construct a custom model based on QAbstractItemModel class. Define the roles you need and use them in your delegates in QML. This way Qt will handle view updates completely automatically.

    There are also other ways to do this, see: link and link.


  • Moderators

    You're doing it backwards. You should update the model used by ListView, then the delegates will update automatically.

    Currently you're just using an integer model, so easiest way to trigger an update is to do:

    view.model = 0
    view.model = alarm.getDisplayEvtCount();
    

    But a much better and future-proof solution is to construct a custom model based on QAbstractItemModel class. Define the roles you need and use them in your delegates in QML. This way Qt will handle view updates completely automatically.

    There are also other ways to do this, see: link and link.



  • Thanks for the reply.

    I will also try to implement custom model based on QAbstractItemModel class as suggested.


Log in to reply