赞
踩
在QML的ListView控件,再填充数据之后,我们有时候还需要对model中的数据进行更改。可是,对model数据进行修改之后,ListView的数据并没有及时刷新出来。甚至有时候forceLayout()也没有作用。
我们可以获取ListView对象的委托item,通过修改item的属性及时达到刷新的目的。
例如QML如下:
ListView { id: layer_list; objectName: "layer_list"; anchors.fill: parent; clip: true; interactive: false; model: modelSource; spacing: 2; delegate: delegateSource; } Component { id: delegateSource; Rectangle { id: wrapper; objectName: "delegateSourceItem"; property string guid: layer_list.model.get(index, 0) property string can_layer_see: layer_list.model.get(index, 4); Image { id: img_see; anchors.right: img_lock.left; anchors.rightMargin: 7; anchors.verticalCenter: parent.verticalCenter; width: 12; height: 12; fillMode: Image.PreserveAspectFit; source: can_layer_see == "true" ? "images/can_see.png" : "images/not_see.png"; } } }
当我们修改img_see的数据时,可以通过一下代码进行修改:
// 动态更新可见性
QQuickItem* content = mpLayerList->property("contentItem").value<QQuickItem*>();
QList<QQuickItem*> list = content->childItems();
for (QQuickItem* item : list) {
std::string on = item->objectName().toStdString();
if (strcmp(on.c_str(), "delegateSourceItem") == 0) {
std::string tempguid = item->property("guid").toString().toStdString();
if (strcmp(tempguid.c_str(), guid.c_str()) == 0) {
item->setProperty("can_layer_see", see.c_str());
LOG(DEBUG) << "Update Signal Source Visibility,GUID: " << guid << ", See:" << see;
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。