7

我有一个 QML ListView,其中委托是真正的 c++ 附加到设备组件,使用线程和QWidgets.

但是当我滚动 ListView 或将委托展开到程序窗口时 - 不可见的委托被破坏。

我可以做些什么来保存我的代表ListModel

我的示例代码:

    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"
        }
    }
}

谢谢!我解决问题!第一种方式 - 我创建 Flickable 对象、Column 和 Repeater,这个解决方案适合我。第二种方式 - 对于主视图中的多个网格(类似 SCADA 的程序),我使用 QObjectList,其中每个对象是 QObjectList,其中每个对象是我的 DataObject :)

4

1 回答 1

5

你可以做些什么来防止 ListView 破坏委托是为cacheBufferListView 的属性分配一个大的值(http://doc-snapshot.qt-project.org/4.8/qml-listview.html#cacheBuffer-prop) . 这显然是一种 hack,而不是推荐的方法。在委托中存储任何状态都是一个坏主意。

相反,您应该做的是在 C++ 模型中分别管理本机对象,您可以根据需要控制每个对象的生命周期。

于 2012-11-06T06:12:14.627 回答