Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable dynamic object-management when visible items in qml listview changes?

I have a QML ListView, where the delegate is real c++ attached to the device component, using threads and QWidgets.

But when I scroll the ListView, or expand the delegate to the program window - not visible delegates are destroyed.

What can I do to save delegates in my ListModel?

My example code:

    import Qt 4.7

    Rectangle {
    width: 320; height: 200; color: "white"
    Text {
        id: debugText
        font.pixelSize: 24
        color:"blue"
        text: "count of created delegates: " + added.toString()
    }
    property int added: 0
    Component {
        id: delegate
        Item {
            id: wrapper
            Component.onCompleted: added += 1
            width: 180; height: 50
            Column {
                x: 5; y: 5
                Text { text: '<b>Name:</b> ' + name }
                Text { text: '<b>Number:</b> ' + number }
            }
        }
    }
    ListView {
        width: parent.width; height: parent.height
        model: contactModel
        delegate: delegate
    }

    ListModel {
        id: contactModel
        ListElement {
            name: "Bill Smith"
            number: "555 3264"
        }
        ListElement {
            name: "John Brown"
            number: "555 8426"
        }
        ListElement {
            name: "Sam Wise"
            number: "555 0473"
        }
        ListElement {
            name: "Bill Smith"
            number: "555 3264"

        }
        ListElement {
            name: "John Brown"
            number: "555 8426"
        }
        ListElement {
            name: "Sam Wise"
            number: "555 0473"
        }
    }
}

Thanks! I solve the problem! First way - i create Flickable object, Column and Repeater, and this solution suits me. Second way - for multiple grids in main view (SCADA-like programm) i use QObjectList, where each Object is QObjectList, where each object is my DataObject :)

like image 712
ctinka Avatar asked Nov 05 '12 20:11

ctinka


1 Answers

What you can do to keep ListView from destroying the delegates is to assign a large value to the cacheBuffer property of the ListView (http://doc-snapshot.qt-project.org/4.8/qml-listview.html#cacheBuffer-prop). This is obviously a hack and not a recommended approach. Storing any state in a delegate is a bad idea.

What you should do instead is manage the native objects separately in a C++ model where you can control the lifespan of each object as you wish.

like image 135
eburger Avatar answered Oct 23 '22 20:10

eburger