访问委托中Repeater组件中的ListView模型

时间:2017-02-01 14:18:34

标签: qt listview qml qt5 repeater

我正在使用ListView与模型和委托。

该模型是一个简单的ListModel,有三个项目。每个项目的值都带有键myFirstRole

委托包含Repeater组件,用于创建任意数量的LabelsLabels必须使用模型中的数据。

转发器的模型不能设置为Listview的模型,因为我使用其他数据Repeater

以下是我想要实现的最小范例:

//MyDelegate.qml
Component {

    Item {
        id: root

        width: childrenRect.width
        height childrenRect.height

        Repeater {
            model: 5 //It's not an option to set the repeaters model to the ListViews model. This example just illustrates my problem.

            Label {
                text: root.ListView.view.model.myFirstRole //This is the line where I want to be able to access the ListView's model, but I can't figure out how to properly reefer to it.
            }
        }
    }
}

//MyListView.qml
ListView {
    id: root

    delegate: MyDelegate {}
    model: ListModel {
        ListElement {
            myFirstRole: "one"
        }
        ListElement {
            myFirstRole: "two"
        }
        ListElement {
            myFirstRole: "three"
        }
    }
}

使用Qt 5.7.0和MSVC2015 32位

2 个答案:

答案 0 :(得分:4)

我认为您无法通过// Dummy list to represent whatever 'Model' is var model = new List<string> { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve" }; // A counter that keeps track of whether we're on 1, 2, 5 or 6 var counter = 1; // Your loop for (int i = 0; i < model.Count; i++) { // Get the item as you've shown var item = model[i]; // Check whether we want this item or not if (counter == 1 || counter == 2 || counter == 5 || counter == 6) { // Display Console.WriteLine("Displayed: " + item); } else { // Do whatever else Console.WriteLine("Skipped: " + item); } // Increase the counter counter++; // Reset the counter if we're into the next batch of 8 if (counter > 8) counter = 1; } 提到的特殊model属性来访问角色here(这是我假设您尝试做的事情){{1 }}。相反,您可以在组件的根级别声明一个属性,然后可以在嵌套范围中使用该属性:

Repeater

如果你有很多属性,这可能会有点乏味。通过快速播放,看起来也可以将整个import QtQuick 2.6 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 ListView { anchors.fill: parent model: ListModel { ListElement { myFirstRole: "Dog" } ListElement { myFirstRole: "Cat" } } delegate: Item { id: root width: childrenRect.width height: childrenRect.height property string myFirstRoleData: myFirstRole Repeater { model: 5 Text { text: myFirstRoleData } } } } } 对象存储在属性中:

model

import QtQuick 2.6 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 ListView { anchors.fill: parent model: ListModel { ListElement { myFirstRole: "Dog" } ListElement { myFirstRole: "Cat" } } delegate: Item { id: root width: childrenRect.width height: childrenRect.height property var modelData: model Repeater { model: 5 Text { text: root.modelData.myFirstRole } } } } } 可能不是最好使用的名称,因为Qt将该名称用于只有一个角色的模型,但是...如果你采用这种方法,你会得到更多不过一个角色。 :)

看起来像Qt Quick Controls'(1)modelData does this too

答案 1 :(得分:1)

//MyDelegate.qml
Item {
    id: root
    property var listViewModel // pass the model data to here
    width: 100
    height: 50

    Column {
        Repeater {
            model: 5 // Use a different model here
            Text {
                width: 50
                height: 10
                text: listViewModel.myFirstRole //This is the line where I want to be able to access the ListView's model, but I can't figure out how to properly reefer to it.
            }
        }
    }
}

//MyListView.qml
ListView {
    id: root
    width: 100
    height: 500

    delegate: MyDelegate {
        listViewModel: model // set the model data here
    }
    model: ListModel {
        ListElement {
            myFirstRole: "one"
        }
        ListElement {
            myFirstRole: "two"
        }
        ListElement {
            myFirstRole: "three"
        }
    }
}

请参阅代码中的注释。猜测你想要实现什么并不是微不足道的,但我希望我猜对了。