【问题标题】:Access ListView model within the Repeater component in the delegate在委托的Repeater组件中访问ListView模型
【发布时间】:2017-02-01 14:18:33
【问题描述】:

我正在使用带有模型和委托的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 位一起使用

【问题讨论】:

  • 您的示例不完整。我根本看不到ListView,但你在标题中提到了这一点。
  • @folibis:我认为没有必要,因为此示例的 ListView 代码只不过是标准样板 ListView 代码。为了完整起见,我可以添加它。

标签: qt listview qml qt5 repeater


【解决方案1】:

我认为您无法通过 Repeater 范围内提到的特殊 model 属性访问角色(我假设您正在尝试这样做)here。相反,您可以在组件的根级别声明一个属性,然后可以在嵌套范围内使用:

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

modelData 可能不是最好使用的名称,因为 Qt 将该名称用于只有一个角色的模型,但是...如果您使用这种方法,您将拥有多个反正角色。 :)

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

【讨论】:

  • 我确实尝试过这个确切的东西,但它没有奏效。如果只是我累了,我会再试一次并报告......
【解决方案2】:
//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"
        }
    }
}

查看代码中的 cmets。猜测你想要达到的目标并非易事,但我希望我猜对了。

【讨论】:

  • 感谢您的建议,但在我的原帖中我在代码中写了一条评论,表示repeater的模型不能设置为listviews模型。原因是中继器模型有一个不同的模型,它必须在我的程序中使用。
  • 好的,我会尝试下一个猜测。您在问题中写的内容......非常令人困惑。
  • 所以改了。我希望它适合您的需求。
  • 是的,这就是我想要实现的。我确实尝试过类似的东西,但我明天会试试这个。我也改变了(希望能改进)这个问题。
  • 嗯,实际上这个解决方案非常适合您的需求。 listViewModel 只是一个属性,它没有连接到您的 delegate.model。因此,您仍然可以通过 modelData.myPrivateData 访问您的委托模型,也可以通过 listViewModel.myFirstRole 访问您的外部 ListView 数据。所以,实际上 listViewModel 不是模型 - 它只是一个对象,包含您从 ListView 传递的数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
相关资源
最近更新 更多