如何在作为中继器的子节点的 gridview 中获取属性

Owen.Geng

我怎样才能得到 gridView.itemAtIndex(index).color?

我尝试过什么:

contentrepeater.itemAt(5).gridView.model.color;
contentrepeater.itemAt(5).gridView.itemAtIndex(5).color;

但它不起作用

Rectangle {
    anchors.top: bar.bottom
    Layout.fillHeight: true
    Layout.fillWidth: true

    Repeater {
        id: contentrepeater
        model: 11
        Rectangle {
            anchors.fill: parent
            color: 'red'
            visible: false

            GridView {
                id: gridView
                anchors.fill: parent
                anchors.topMargin: 10
                anchors.leftMargin: 10
                cellWidth: 150
                cellHeight: 170
                clip: true

                model: 11

                delegate: Rectangle{
                    height: 160
                    width: 140
                    color: '#333333'
                }
            }
        }
    }
}
马克西姆·帕普诺

最终,您可能不想那样做。这将是hackish和容易出错的。例如,GridView仅提供基于位置坐标的项目访问,而不是索引。因此,您需要深入了解将动态创建的子项……这是可能的,但非常混乱,并且没有真正支持的 API。

你最好先定义你的项目模型,然后使用GridView(或其他)来显示它们。这样您就可以操作模型中的对象,更改将反映在视图中(而不是像您现在尝试的那样)。

此示例(基于您发布的代码)创建了 4 个布局,每个布局有 11 个方格,并使用定时脚本为每个方格中的颜色设置动画。请注意,对于其中的每个GridViews ,我们都需要单独的模型实例contentrepeater(否则它只会显示在最后一个视图中)。所以这个例子有点复杂,因为项目模型是动态创建的。

我应该在“真实”应用程序中添加这一点,我将使用不同的方法来跟踪创建的项目模型,而不是像我在这里那样在显示层次结构中查找它们。这试图演示的要点是通过更改模型数据来操作显示的项目(委托)。

import QtQuick 2.9
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
import QtQml.Models 2.3

Pane {
    id: root
    width: 400
    height: 650
    padding: 9

    // rectangle items to create per model
    property int itemsPerModel: 11
    // prototype object model
    property Component itemModel: ObjectModel {}
    // prototype model item
    property Component delegate: Rectangle {
        height: 30
        width: 30
        color: '#333333'
    }

    // Creates a new ObjectModel with some Rectangle items as children
    function newItemModel() {
        var model = itemModel.createObject(root);
        for (var i=0; i < itemsPerModel; ++i)
            model.append(delegate.createObject(root));
        return model;
    }

    SequentialAnimation {
        id: animate
        running: true
        loops: Animation.Infinite
        ScriptAction {
            property string nextColor: "blue"
            property int nextSet: 0
            property int nextItem: 0
            script: {
                contentrepeater.itemAt(nextSet)  // the Rectangle within the GridLayout
                    .children[0]                 // the gridView within the Rectangle
                    .model.get(nextItem)         // the model's delegate item (a Rectangle)
                    .color = nextColor;          // set the new color on it.

                // advance to next item or set of items.
                nextItem = (nextItem+1) % root.itemsPerModel;
                if (!nextItem)
                    nextSet = (nextSet+1) % contentrepeater.count;
                nextColor = (nextColor === "blue" ? "orange" : nextColor === "orange" ? "white" : "blue");
            }
        }
        PauseAnimation { duration: 100 }
    }

    GridLayout {
        columns: 2
        anchors.fill: parent

        Repeater {
            id: contentrepeater
            model: 4

            Rectangle {
                color: 'red'
                width: 150
                height: 170

                GridView {
                    id: gridView
                    anchors.fill: parent
                    anchors.topMargin: 10
                    anchors.leftMargin: 10
                    cellWidth: 40
                    cellHeight: 40
                    clip: true

                    // here we need a unique instance of the ObjectModel
                    model: root.newItemModel()
                }
            }
        }
    }
}

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在子中继器的 ItemCommand 事件中获取当前中继器

来自分类Dev

如何像中继器一样运行GridView?

来自分类Dev

如何在中继器中显示基于值的指令

来自分类Dev

如何在中继器的hyperLink navigationUrl中传递参数

来自分类Dev

如何在gridview中获取templatefield的值作为数组

来自分类Dev

如何在javascript中的中继器中获取特定标签的文本

来自分类Dev

如何在javascript中的中继器中获取特定标签的文本

来自分类Dev

如何在中继器控件中获取链接按钮生成的文本

来自分类Dev

如何从Gridview中的templatefields中获取值

来自分类Dev

如何从GridView获取数据

来自分类Dev

如何从 GridView 获取位置

来自分类Dev

在gridview中获取属性“行”时出错

来自分类Dev

如何对中继器中的列求和

来自分类Dev

如何从gridview中的edittext获取值

来自分类Dev

我想从代码中的中继器获取drowpdown值,如何在asp.net中获取它?

来自分类Dev

如何在javascript中的gridview中获取新输入的值

来自分类Dev

如何在中继器中的另一个 div 悬停时将类应用于中继器中的 div

来自分类Dev

如何在Web窗体中访问GridView Templete字段的属性

来自分类Dev

如何在Fuel UX中继器中重新加载数据

来自分类Dev

如何在中继器中更改列(数据集)值名称

来自分类Dev

如何在中继器控件中垂直而不是水平地显示图像行

来自分类Dev

如何在C#中的循环中绑定中继器

来自分类Dev

如何在OctoberCMS中为中继器的内部字段设置所需的验证?

来自分类Dev

如何在rowupdating事件中获取gridview的boundfield值

来自分类Dev

如何在Javascript或JQuery的Gridview中获取选定的行

来自分类Dev

如何在GridView中获取主键但不显示它?

来自分类Dev

如何在GridView中获取主键但不显示它?

来自分类Dev

如何在Krajee GridView中获取字段的值?

来自分类Dev

如何在GridView中获取编辑文本框的ID

Related 相关文章

  1. 1

    如何在子中继器的 ItemCommand 事件中获取当前中继器

  2. 2

    如何像中继器一样运行GridView?

  3. 3

    如何在中继器中显示基于值的指令

  4. 4

    如何在中继器的hyperLink navigationUrl中传递参数

  5. 5

    如何在gridview中获取templatefield的值作为数组

  6. 6

    如何在javascript中的中继器中获取特定标签的文本

  7. 7

    如何在javascript中的中继器中获取特定标签的文本

  8. 8

    如何在中继器控件中获取链接按钮生成的文本

  9. 9

    如何从Gridview中的templatefields中获取值

  10. 10

    如何从GridView获取数据

  11. 11

    如何从 GridView 获取位置

  12. 12

    在gridview中获取属性“行”时出错

  13. 13

    如何对中继器中的列求和

  14. 14

    如何从gridview中的edittext获取值

  15. 15

    我想从代码中的中继器获取drowpdown值,如何在asp.net中获取它?

  16. 16

    如何在javascript中的gridview中获取新输入的值

  17. 17

    如何在中继器中的另一个 div 悬停时将类应用于中继器中的 div

  18. 18

    如何在Web窗体中访问GridView Templete字段的属性

  19. 19

    如何在Fuel UX中继器中重新加载数据

  20. 20

    如何在中继器中更改列(数据集)值名称

  21. 21

    如何在中继器控件中垂直而不是水平地显示图像行

  22. 22

    如何在C#中的循环中绑定中继器

  23. 23

    如何在OctoberCMS中为中继器的内部字段设置所需的验证?

  24. 24

    如何在rowupdating事件中获取gridview的boundfield值

  25. 25

    如何在Javascript或JQuery的Gridview中获取选定的行

  26. 26

    如何在GridView中获取主键但不显示它?

  27. 27

    如何在GridView中获取主键但不显示它?

  28. 28

    如何在Krajee GridView中获取字段的值?

  29. 29

    如何在GridView中获取编辑文本框的ID

热门标签

归档