首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态创建ListModel列表

动态创建ListModel列表
EN

Stack Overflow用户
提问于 2017-02-08 03:36:41
回答 1查看 780关注 0票数 1

我想有一个Swipeview,在一个卷帘视图的小部件(按钮)列表。所有内容都必须动态创建,因为可以有一个或五个屏幕,或者一个或多个小部件。可以通过抽屉添加(以后)。在抽屉上添加屏幕增加了另一个屏幕。添加小部件在当前屏幕上添加一个小部件。我认为它应该和图像上的一样。我试着让这一切像here一样。

我的尝试是:

main.qml

代码语言:javascript
复制
ApplicationWindow {
visible: true
Item {
    id: root

    ListModel {
        id: listoflists
    }

    ListView {
        id: listview 
    }

    function addlist() {
        CreateObject.create("listofwidgets.qml", root, itemAdded);
    }

    function listadded(obj, source) {
        listoflists.append({"obj": obj, "source": source})
    }

    function addview() {
        CreateObject.create("view.qml", root, itemAdded);
    }

    function viewadded(obj, source) {
        listoflists.append({"obj": obj, "source": source})
    }

}

Component {
    id: modelDelegate
        Text {
            text: name
        }
}

SwipeView {
    id: view
    currentIndex: 0

}

PageIndicator {
    id: indicator

    count: view.count
    currentIndex: view.currentIndex

    anchors.bottom: view.bottom
    anchors.horizontalCenter: parent.horizontalCenter
}

Button {
    width: parent.width
    height: 20
    text: "add screen"

    onClicked: {
        addlist()
        addview()
    }
}

Button {
    width: parent.width
    height: 20
    text: "add widget"

    onClicked: {
         listoflist.get(view.currentIndex).addwidget()
    }
}
}

listofwidgets.qml

代码语言:javascript
复制
Item {
id: root2

ListModel {
    id: listofwidgets
}

function addwidget() {
    CreateObject.create("widget.qml", root2, widgetadded);
}

function widgetadded(obj, source) {
    listofwidgets.append({"obj": obj, "source": source})
}

}

widget.qml

代码语言:javascript
复制
ListElement {
    name: ""
}

view.qml

代码语言:javascript
复制
Item {
id: view.currentIndex

ListView {
    model: listoflists.get(view.currentIndex)
    delegate: modelDelegate

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 15:25:19

我不知道这是否是您需要的,但这里有一个完全动态创建的SwipeView示例

代码语言:javascript
复制
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

Window {
    width: 400
    height: 600
    visible: true

    ColumnLayout {
        anchors.fill: parent
        spacing: 2
        Rectangle {
            Layout.preferredHeight: 50
            Layout.fillWidth: true
            Row {
               Button {
                   text: "Add screen"
                   onClicked: addScreen();
               }
               Button {
                   text: "Add widget"
                   onClicked: addWidget();
               }
            }
        }

        SwipeView {
            id: swipe
            Layout.fillWidth: true
            Layout.fillHeight: true
        }
    }

    Component {
        id: screenComponent
        Rectangle {
            property alias view: view
            color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
            ListView {
                id: view
                spacing: 2
                anchors.fill: parent
                model: ListModel {}
                delegate: widgetComponent
            }
        }
    }
    Component {
        id: widgetComponent
        Button {
            text: name + "_" + index
        }
    }

    function addScreen()
    {
        var page = screenComponent.createObject(swipe);
        swipe.addItem(page);
        swipe.currentIndex = swipe.count - 1;
    }

    function addWidget()
    {
        var page = swipe.currentItem;
        if(page) {
            page.view.model.append({name: "widget"});
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42098456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档