Qt 基于ListModel计数动态创建页面

Qt 基于ListModel计数动态创建页面,qt,qml,grid-layout,loader,Qt,Qml,Grid Layout,Loader,我是QT/QML应用程序开发的初学者 如何基于ListModel计数动态创建qml。 在视图中,我使用Repeater在GridLayout中列出了modelObjects Item{ id:griditem anchors.fill:parent GridLayout{ id: grid2 x:145 y:30 Layout.pr

我是QT/QML应用程序开发的初学者

如何基于ListModel计数动态创建qml。 在视图中,我使用Repeater在GridLayout中列出了modelObjects

Item{
      id:griditem
      anchors.fill:parent

          GridLayout{
               id: grid2
               x:145
               y:30
               Layout.preferredHeight: 480
               Layout.preferredWidth: 1135
               rowSpacing:10
               columnSpacing:40

               columns: 3
               rows: 2

               Repeater{
                   id: repeater_Id
                   model: FeatureModel{}

                   Loader{
                       id: loader_Id

                       source: "QuadTiles.qml"
                       onLoaded: {
                             loader_Id.item.nIndex=index
                             loader_Id.item.type_String = type

                             loader_Id.item.title_Text.text = title
                             loader_Id.item.description_Text.text = description
                             loader_Id.item.btn1_icon.source = icon1

                       }
                   }
               } //Repeater
          }//GridLayout
  }
编辑: 我面临一些问题 我需要根据ModelList计数动态创建新视图。在GridLayout中,每页最多有6项(3行2列)


“QuadTiles.qml”是加载到GridLayout的每个项目中的qml文件

尝试以下操作:
lm
是要拆分的
ListModel

SwipeView {
    width: 200
    height: 800
    clip: true
    currentIndex: 0

    Repeater {
        model: Math.ceil(lm.count / 6)
        delegate:            ListView {
            width: 200
            height: 800
            property int viewIndex: index
            model: DelegateModel {
                model: lm
                groups: DelegateModelGroup { name: 'filter' }
                Component.onCompleted: {
                    for (var i = viewIndex * 6; i < lm.count && i < (viewIndex * 6) + 6; i++) {
                        items.setGroups(i, 1, ['items', 'filter'])
                    }
                }

                filterOnGroup: 'filter'

                delegate: Rectangle {
                    width: 180
                    height: 30
                    border.width: 1
                    Text {
                        anchors.centerIn: parent
                        text: index
                    }
                }
            }
        }
    }
}
SwipeView{
宽度:200
身高:800
剪辑:对
当前索引:0
中继器{
模型:Math.ceil(lm.count/6)
代表:ListView{
宽度:200
身高:800
属性int viewIndex:索引
模型:委派模型{
型号:lm
组:DelegateModelGroup{name:'filter'}
Component.onCompleted:{
对于(var i=viewIndex*6;i

不要使用
加载程序作为委托。委托是动态实例化的,因此
加载程序
只是无用的开销。您可以在代理中使用
加载器
来处理通常未显示的部分。

我完全不了解您试图实现的目标。什么是页面?为什么要使用
加载程序
?@derM:我想动态创建视图。每个视图都有网格,每个网格最多可容纳6个项目。如果项目计数超过6,我需要在另一个带有滑动和页面指示器的视图中显示项目。在滑动视图中,我动态创建了GridView。但是,我在SwipeView中滚动到第二个视图时遇到了一个问题。也就是说,当滚动到第二页时,将显示第一屏内容。如何解决此问题。set
clip:true
您还应该手动更新中继器的型号(count/6)。由于“modelChanged”-信号仅会发出,因此如果值发生更改,则在新页面已满之前不会发出该信号。有两种方法,我可以想到,来做到这一点。我已经为gridView设置了clip属性,但没有成功:(是的,在SwipeView中设置它。我相应地更改了示例。