【问题标题】:Qt Quick QML GridLayout with multiple rows and columns using JSON使用 JSON 的具有多行和多列的 Qt Quick QML GridLayout
【发布时间】:2020-05-27 20:48:34
【问题描述】:

我想要在 GridLayout 中显示以下 JSON。
此 JSON 在 QML 文件中接收并分配给属性。

{
    {
        "id":1
        "name": "abc1"
        "age": 10
        "city": "xvy"
        "gender": "m/f"
    },
    {
        "id":2
        "name": "abc2"
        "age": 10
        "city": "xvy"
        "gender": "m/f"
    },
    {
        "id":2
        "name": "abc3"
        "age": 10
        "city": "xvy"
        "gender": "m/f"
    }
}

我想在 5-Columns 和 3-Rows GridLayout 中显示此 JSON。
如下表所示:

+---------+--------------------+-----------------+-----------------+----------------+
|   Id    |  Name              |  Age            |    City         |    Gender      |
+---------+--------------------+-----------------+-----------------+----------------+
|    1    |    TextFiled       | TextFiled       | TextFiled       | ComboBox       |
+---------+--------------------+-----------------+-----------------+----------------+
|    2    |    TextFiled       | TextFiled       | TextFiled       | ComboBox       | 
+---------+--------------------+-----------------+-----------------+----------------+
|    3    |    TextFiled       | TextFiled       | TextFiled       | ComboBox       |
+---------+--------------------+-----------------+-----------------+----------------+

到目前为止,我已经想到了以下解决方案:

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4

Window {
    width: 600; height: 400; visible: true

    GridLayout {
        id: grid
        anchors.fill: parent
        columns: 5
        rowSpacing: 2
        columnSpacing: 2
        anchors.margins: 5

        // example models
        property var id: [ 1, 2, 3, 4, 5 ]
        property var name: [ "value1", "value2", "value3", "value4", "value5" ]
        property var age: [ 10, 20, 30, 40, 50 ]
        property var city: [ "value1", "value2", "value3", "value4", "value5" ]
        property var gender: [ "m", "f", "m", "f", "m" ]

        Repeater {
            model: grid.id
            Label {
                Layout.row: index
                Layout.column: 0
                Layout.fillWidth: true
                Layout.fillHeight: true
                text: modelData
            }
        }

        Repeater {
            model: grid.name
            TextFiled {
                Layout.row: index
                Layout.column: 1
                Layout.fillWidth: true
                Layout.fillHeight: true
                text: modelData
            }
        }

        Repeater {
            model: grid.age
            TextFiled {
                Layout.row: index
                Layout.column: 2
                Layout.fillWidth: true
                Layout.fillHeight: true
                text: modelData
            }
        }


        // And rest of Repeater for next columns 
        // ....

    }
}

这是正确的方法吗?
此外,我想将 ComboBox 和 TextFiled 中的更新值从网格保存到 JSON,我该怎么做?

【问题讨论】:

  • 我认为您可以通过在两级中继器设置中使用Object.keys() 函数来使显示工作
  • 如果你有一个合适的模型,可以使用新的 TableView 和 DelegateChooser 作为代理。
  • @Amfasis:我不太清楚 Object.keys() 是什么,请您详细解释一下。
  • @Mitch:我认为 DelegateChooser 适合这个要求,但现在不确定如何在这种情况下使用它,因为我是 QML 新手。我还想保存用户更新后的输入值。
  • Object.keys() 方法为您提供了一个对象(例如第一个对象)中所有键的列表,您可以将其提供给中继器,这样中继器将为每一列实例化委托你有

标签: qt qml qtquick2 grid-layout qt-quick


【解决方案1】:

假设您提供了不正确的 JSON(我已修复),我将使用以下解决方案。相同的解决方案可以基于 Object.keys(),正如 @Amfasis 已经提到的,但在这种情况下,所有属性在所有数组对象中的顺序都应该相同。

Component {
    id: tableHeader
    Text {
        text: modelData
        font.bold: true
    }
}

Component {
    id: tableCell
    Repeater {
        property var arr: modelData
        model: layout.cols.length
        delegate: Text {
            text: arr[layout.cols[index]]
        }
    }
}

GridLayout {
    id: layout
    columns: 5
    anchors.fill: parent
    anchors.margins: 10
    property var cols: ['id','name','age','city','gender']
    property var json: [
        {
            "id": 1,
            "name": "abc1",
            "age": 10,
            "city": "xvy",
            "gender": "m/f"
        },
        {
            "id": 2,
            "name": "abc2",
            "age": 20,
            "city": "xvy",
            "gender": "m/f"
        },
        {
            "id": 3,
            "name": "abc3",
            "age": 30,
            "city": "xvy",
            "gender": "m/f"
        }
    ]

    Repeater {
        model: layout.cols
        delegate: tableHeader
    }

    Repeater {
        model: layout.json
        delegate: tableCell
    }
}

【讨论】:

  • 这将生成只有文本的表格单元格,我需要在一行中生成具有不同组件的单元格,如提到的文本、TextFiled 和 ComboBox 以及问题中给出的示例表。我还需要将单元格中的更新值保存到目标 JSON。
  • 您没有提到这一点,但无论如何,您可以根据列索引使用不同的委托组件。
猜你喜欢
  • 2013-11-10
  • 2016-01-16
  • 2023-01-09
  • 1970-01-01
  • 2023-03-29
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多