aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlmodels/doc/snippets/qml/tablemodel/fruit-example-complex.qml
blob: 70ce56f5998c0f251bc7c81ec10b7fe5b8e888e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

//![file]
import QtQuick
import QtQuick.Window
import Qt.labs.qmlmodels

Window {
    width: 400
    height: 400
    visible: true

    TableView {
        anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        boundsBehavior: Flickable.StopAtBounds

        model: TableModel {
            TableModelColumn {
                display: function(modelIndex) { return rows[modelIndex.row][0].checked }
                setDisplay: function(modelIndex, cellData) { rows[modelIndex.row][0].checked = cellData }
            }
            TableModelColumn {
                display: function(modelIndex) { return rows[modelIndex.row][1].amount }
                setDisplay: function(modelIndex, cellData) { rows[modelIndex.row][1].amount = cellData }
            }
            TableModelColumn {
                display: function(modelIndex) { return rows[modelIndex.row][2].fruitType }
                setDisplay: function(modelIndex, cellData) { rows[modelIndex.row][2].fruitType = cellData }
            }
            TableModelColumn {
                display: function(modelIndex) { return rows[modelIndex.row][3].fruitName }
                setDisplay: function(modelIndex, cellData) { rows[modelIndex.row][3].fruitName = cellData }
            }
            TableModelColumn {
                display: function(modelIndex) { return rows[modelIndex.row][4].fruitPrice }
                setDisplay: function(modelIndex, cellData) { rows[modelIndex.row][4].fruitPrice = cellData }
            }

            // Each row is one type of fruit that can be ordered
//![rows]
            rows: [
                [
                    // Each object (line) is one cell/column.
                    { checked: false, checkable: true },
                    { amount: 1 },
                    { fruitType: "Apple" },
                    { fruitName: "Granny Smith" },
                    { fruitPrice: 1.50 }
                ],
                [
                    { checked: true, checkable: true },
                    { amount: 4 },
                    { fruitType: "Orange" },
                    { fruitName: "Navel" },
                    { fruitPrice: 2.50 }
                ],
                [
                    { checked: false, checkable: false },
                    { amount: 1 },
                    { fruitType: "Banana" },
                    { fruitName: "Cavendish" },
                    { fruitPrice: 3.50 }
                ]
            ]
//![rows]
        }
//![delegate]
        delegate:  TextInput {
            text: model.display
            padding: 12
            selectByMouse: true

            onAccepted: model.display = text

            Rectangle {
                anchors.fill: parent
                color: "#efefef"
                z: -1
            }
        }
//![delegate]
    }
}
//![file]