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

ListView - how to add new item



  • I have simple listview. I am trying to use "append" to add new item, but item is added at index 0. How to add new item in next line?

    
    Window
    {
        id: alarmListWindow
    
        width: 400
        height: 400
    
        ListModel
        {
            id: modelID
    
    //        ListElement
    //        {
    //            idAlarm: ""
    //            typeAlarm: ""
    //            messageAlarm: ""
    //        }
        }
        Component
        {
            id: delegatID
            Rectangle
            {
                id: alarmItemDelegate
                x: 0
                y: 0
                Text
                {
                    id: idAl
                    y: parent.y
                    text: "ID: " + model.idAlarm
                }
                Text
                {
                    id: messAl
                    y: idAl.height
                    text: "message: " + model.messageAlarm
                }
            }
        }
        ListView
        {
            id: listviewID
            anchors.fill: parent
            width: 180; height: 200
            model: modelID
            delegate: delegatID
        }
        Component.onCompleted:
        {
    //        modelID.append( { "idAlarm": "12345678", "typeAlarm": "TV", "messageAlarm": "Jakas wiadomosc" } )
        }
        Timer
        {
            id: timerUpdateListView
            interval: 1000; running: true; repeat: true; triggeredOnStart: true
            onTriggered: updateListView()
        }
        function updateListView()
        {
            modelID.append( { "idAlarm": "alarm.timer_remaining", "typeAlarm": "TT", "messageAlarm": "Jakas wiadomosc" } )
        }
    }
    
    


  • @PawlosCK Hi - your list is being appended to. The problem is in its display in the ListView. You need to size your item delegate.

    For example if you add

           Rectangle
            {
                id: alarmItemDelegate
                //x: 0 NOT NEEDED??
                //y: 0
                height: 40               \\ <<<<
                width: listviewID.width  \\ <<<<
                Text
                {
                    id: idAl
                    y: parent.y
                    text: "ID: " + model.idAlarm
                }
                ... 
    

Log in to reply