垂直listview内的水平listview模型

时间:2016-06-01 09:23:56

标签: c++ listview model qml qt5

我想将水平列表视图作为垂直列表视图的委托。是否可以将不同的模型附加到c ++的水平列表视图中?

 ListView {
            id: eventList
            model: N (N tells how many horizontal lists are there)
            delegate:
                ListView {
                id: horizontall
                model: ?? // How to attach different model from C++ to each horizontal list
                orientation: ListView.Horizontal

在c ++部分,我实现了从QAbstractlistmodel派生的N个QList列表

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。无法使ListModel包含另一个ListModel。至少在QML中是不可能的。

这是如何制作它的快速而肮脏的例子。它能解决你的问题吗?

import QtQuick 2.4
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.4
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480

    ListModel {
        id: listModel
        ListElement {slmIndex: 0}
        ListElement {slmIndex: 1}
        ListElement {slmIndex: 2}
        ListElement {slmIndex: 3}
        ListElement {slmIndex: 4}
        ListElement {slmIndex: 5}
    }

    ListModel {
        id: model1
        ListElement { name: "1" }
    }
    ListModel {
        id: model2
        ListElement { name: "1" }
        ListElement { name: "2" }
    }
    ListModel {
        id: model3
        ListElement { name: "1" }
        ListElement { name: "2" }
        ListElement { name: "3" }
    }
    ListModel {
        id: model4
        ListElement { name: "1" }
        ListElement { name: "2" }
        ListElement { name: "3" }
        ListElement { name: "4" }
    }
    ListModel {
        id: model5
        ListElement { name: "1" }
        ListElement { name: "2" }
        ListElement { name: "3" }
        ListElement { name: "4" }
        ListElement { name: "5" }
    }
    ListModel {
        id: model6
        ListElement { name: "1" }
        ListElement { name: "2" }
        ListElement { name: "3" }
        ListElement { name: "4" }
        ListElement { name: "5" }
        ListElement { name: "6" }
    }
    property var secondLayerModels: [
        model1, model2, model3, model4, model5, model6
    ]

    ListView {
        anchors.centerIn: parent
        width: 200
        height: 200
        clip: true
        model: listModel
        orientation: ListView.Vertical
        delegate: ListView {
            width: 200
            height: 50
            model: secondLayerModels[slmIndex]
            orientation: ListView.Horizontal
            delegate: Rectangle {
                width: 50
                height: 50
                border.color: "black"
                Text {
                    anchors.fill: parent
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    text: name
                }
            }
        }
    }
}

我不确定这个答案是否完全涵盖了这个问题。它没有介绍如何创建将公开其他模型的C ++模型。