aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quickcontrols/gallery/pages/TableViewPage.qml
blob: e9ebfea64e4e2fec2b3f70cc8b775cead569eb21 (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
88
89
90
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Qt.labs.qmlmodels

Page {
    id: page
    enabled: !GalleryConfig.disabled

    GridLayout {
        anchors.fill: parent

        Label {
            wrapMode: Label.Wrap
            horizontalAlignment: Qt.AlignHCenter
            text: qsTr("TableView provides a scrollable grid that displays data from "
                       + "a model in rows and columns, allowing users to view and interact "
                       + "with structured information within an application.")
            Layout.fillWidth: true
            Layout.columnSpan: 2
        }

        HorizontalHeaderView {
            clip: true
            syncView: tableView
            model: tableModel.headerModel
            Layout.column: 1
            Layout.row: 1
            Layout.fillWidth: true
        }

        VerticalHeaderView {
            clip: true
            syncView: tableView
            Layout.column: 0
            Layout.row: 2
            Layout.fillHeight: true
        }

        TableView {
            id: tableView
            columnSpacing: 1
            rowSpacing: 1
            clip: true

            selectionModel: ItemSelectionModel {}
            model: tableModel

            Layout.column: 1
            Layout.row: 2
            Layout.fillWidth: true
            Layout.fillHeight: true

            delegate: TableViewDelegate {
                implicitWidth: 100
                implicitHeight: 50
                Component.onCompleted: {
                    if (contentItem as Label) {
                        contentItem.horizontalAlignment = Qt.AlignHCenter
                        contentItem.verticalAlignment = Qt.AlignVCenter
                    }
                }
            }
        }
    }

    TableModel {
        id: tableModel
        property var headerModel: [qsTr("Name"), qsTr("Color")]
        TableModelColumn { display: "name" }
        TableModelColumn { display: "color" }
        rows: [
            {
                "name": qsTr("cat"),
                "color": qsTr("black")
            },
            {
                "name": qsTr("dog"),
                "color": qsTr("brown")
            },
            {
                "name": qsTr("bird"),
                "color": qsTr("white")
            }
        ]
    }
}