如何解决如何通过XMLListModel中的属性在动态组件中创建动态对象和不同对象?
我从文件Page.qml动态创建了一个页面,对此没有问题。 在此文件中,我有一个XMLListModel,它从该XML文件中获取标签。
elements.xml
<element>First
<type>Video</type>
<source>videos.mp4<source>
</element>
<element>Second
<type>Image</type>
<source>image.jpg</source>
</element>
Page.qml
...
Page
{
XMLListModel
{
id:xmlmodel
source:"elements.xml"
query:"/element"
XmlRole
{
name:"type"; query:"type/string()"
}
XmlRole
{
name:"source"; query:"type/source()"
}
...}
我要在javascript中创建一个函数,其中:
- 我可以像传递XmlListModel类型和源一样传递参数;
- 在相对qml文件(videos.qml,image.qml)中的qml创建对象类型和源之间进行切换的开关,以像添加属性一样
- 如果可能的话,我想将此对象放在GridView中。 但我不知道该怎么办。
解决方法
您可以将 xmlmodel 分配给GridView的模型属性。
从 video.qml 和 image.qml 创建组件对象,这些对象将用作委托。添加了用于处理错误类型的额外组件(compPlaceHolder)。
Component{ id: compVideo; Videos{}}
Component{ id: compImage; id: Image{}}
Component{ id: compPlaceHolder; id: Item{}}
对于GridView的委托,您可以使用Loader组件,该组件将根据以下参数动态加载组件 compVideo , compImage 或 compPlaceHolder 。模型中的“类型”属性。
Loader{
sourceComponent: assignDel()
function assignDel(){
if(type == "Video"){ return compVideo; }
else if(type == "Image"){ return compImage; }
else{ return compPlaceHolder; }
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。