根据ListModel计数动态创建页面 [英] Dynamically create page based on ListModel count

查看:146
本文介绍了根据ListModel计数动态创建页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a beginner QT/QML app development

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

How can I create a qml dynamically based on the ListModel count. In the view I am listing the modelObjects in a GridLayout using Repeater.

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列)

Edit : I am facing some issues I need to create new views dynamically based on the ModelList count. Each page having maximum 6 item (3 rows and 2 columns) in GridLayout

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

'QuadTiles.qml' is the qml file which is load in to each item of GridLayout

推荐答案

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

Try something like this:
lm is the ListModel that is to be split.

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
                    }
                }
            }
        }
    }
}

也不要使用Loader作为委托.委托是动态实例化的,因此Loader只是无用的开销.您可能在委托中使用Loader来表示通常不显示的部分.

And don't use a Loader as a delegate. The delegates are instantiated dynamically, so the Loader is just useless overhead. You might use a Loader within your delegate for parts, that are usually not shown.

这篇关于根据ListModel计数动态创建页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆