GridView {
id: gridv
model: ListModel {
id: modelone;
}
delegate: componentId
}
Rectangle {
id: whattheproblem
color: red
ListView {
id: listv
model: ListModel {
id: modeltwo;
}
delegate: anotherComponentId
}
}
我可以gridv.model.append(element)
,它会向显示的GridView添加元素。
但是,我不能做listv.model.append(element)
,它不会绘制任何东西(组件代码是有效的),但与此同时,modeltwo.count
显示该元素已添加到模型中。添加了Rectangle
来检查布局(目前由RowLayout管理),它似乎正在工作;其他布局的东西(想想anchor
,x/y/z
)没有帮助。
QT 5.3,QtQuick 2 ..
从我的观点来看,我现在只能想到,modelone
将所有ListModel逻辑与它创建的GridView相关联,因此ListModel不能再与ListView一起使用了。听起来不合逻辑,但已经花了两个小时。
在处理多个视图时是否有必要创建自定义模型?
答案 0 :(得分:0)
我认为问题出在代理人身上,因为我尝试了一些修改后的例子并且有效。 以下是代码:
Item {
width: 200
height: 200
GridView {
id: gridv
width: 200
height: 100
model: ListModel {
id: modelone;
}
delegate: Text { text: name }
}
Rectangle {
id: whattheproblem
anchors.top: gridv.bottom
ListView {
id: listv
model: ListModel {
id: modeltwo;
}
delegate: Text { text: name }
}
}
Button {
anchors.left: parent.left
anchors.bottom: parent.bottom
text: "Add to Grid"
onClicked: gridv.model.append({name: "grid"})
}
Button {
anchors.right: parent.right
anchors.bottom: parent.bottom
text: "Add to List"
onClicked: listv.model.append({name: "list"})
}
}
我用Qt 5.3.1
尝试了