【发布时间】:2014-02-26 08:51:32
【问题描述】:
我正在使用 QtQuick 2.0 和 QML ListView,我用 C++ 连接到我的模型(对象的 QList)。连接是通过 QQmlContext::setContextProperty() 建立的。
现在文档告诉我,界面无法直接了解更改,因此我只在更改模型时才实现上下文。但是,当我这样做时,视图会直接实现而不触发任何事件(例如添加或删除事件),这让我有点恼火,因为我无法控制转换。
简单来说就是我的qml代码:
ListView {
id : list
boundsBehavior: Flickable.StopAtBounds
anchors {
top: titleBar.bottom
topMargin: -1
bottom: mainWindow.bottom
bottomMargin: -1
}
width: mainWindow.width
model: episodes
delegate: Episode {
id: myDelegate
onShowClicked: episodes.append(episodes[index])
}
ScrollBar {
flickable: list;
}
}
Episode 是我的自定义委托。它包含以下代码:
ListView.onAdd: SequentialAnimation {
PropertyAction { target: episodeDelegate; property: "height"; value: 0 }
NumberAnimation { target: episodeDelegate; property: "height"; to: 80; duration: 250; easing.type: Easing.InOutQuad }
}
ListView.onRemove: SequentialAnimation {
PropertyAction { target: episodeDelegate; property: "ListView.delayRemove"; value: true }
NumberAnimation { target: episodeDelegate; property: "height"; to: 0; duration: 250; easing.type: Easing.InOutQuad }
// Make sure delayRemove is set back to false so that the item can be destroyed
PropertyAction { target: episodeDelegate; property: "ListView.delayRemove"; value: false }
}
这是 Qt 示例的直接副本。
总而言之,模型已正确链接和同步,但这样做的方式使我无法了解 QML 逻辑中模型更改的性质。
有人知道什么技巧吗?
【问题讨论】: