TreeModel QML Type

単純なツリーモデルをカプセル化します。詳細...

Import Statement: import Qt.labs.qmlmodels
Since: Qt 6.10

プロパティ

方法

  • appendRow(object treeRow)
  • appendRow(QModelIndex parent, object treeRow)
  • clear()
  • variant data(QModelIndex index, string role)
  • object getRow(const QModelIndex &rowIndex)
  • QModelIndex index(list<int> treeIndex, int column)
  • QModelIndex index(int row, int column, object parent)
  • removeRow(QModelIndex rowIndex)
  • bool setData(QModelIndex index, variant value, string role)
  • setRow(QModelIndex rowIndex, object treeRow)

詳細説明

TreeModel 型は、TreeView で使用できるツリーモデルのデータとして、JavaScript/JSON オブジェクトを格納します。これは、C++ でカスタムQAbstractItemModel サブクラスを作成することなく、非常に単純なモデルをサポートすることを目的としています。

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

ApplicationWindow {
    visible: true
    width: 500
    height: 500

    TreeView {
        id: treeView
        anchors.fill: parent

        selectionModel: ItemSelectionModel {}

        model: TreeModel {
            id: treeModel

            TableModelColumn {
                display: "checked"
            }
            TableModelColumn {
                display: "size"
            }
            TableModelColumn {
                display: "type"
            }
            TableModelColumn {
                display: "name"
            }
            TableModelColumn {
                display: "lastModified"
            }

            rows: [{
                    checked: false,
                    size: "—",
                    type: "folder",
                    name: "Documents",
                    lastModified: "2025-07-01",
                    rows: [{
                            checked: true,
                            size: "24 KB",
                            type: "file",
                            name: "Resume.pdf",
                            lastModified: "2025-06-20",
                        }, {
                            checked: false,
                            size: "2 MB",
                            type: "folder",
                            name: "Reports",
                            lastModified: "2025-06-10",
                            rows: [{
                                    checked: true,
                                    size: "850 KB",
                                    type: "file",
                                    name: "Q2_Report.docx",
                                    lastModified: "2025-06-15",
                                }, {
                                    checked: false,
                                    size: "1.2 MB",
                                    type: "file",
                                    name: "Q3_Plan.xlsx",
                                    lastModified: "2025-06-18",
                                }]
                        }]
                },
                {
                    checked: false,
                    size: "—",
                    type: "folder",
                    name: "Pictures",
                    lastModified: "2025-05-30",
                    rows: [{
                            checked: true,
                            size: "3.5 MB",
                            type: "file",
                            name: "Vacation.jpg",
                            lastModified: "2025-05-15",
                        }, {
                            checked: false,
                            size: "2.1 MB",
                            type: "file",
                            name: "Family.png",
                            lastModified: "2025-05-20",
                        }]
                }
            ]
        }

        delegate: TreeViewDelegate {}
    }
}

モデルの初期データは、rows プロパティを使用するか、appendRow ()を呼び出すことで設定されます。モデル内の各列は、TableModelColumn インスタンスを宣言することで指定されます。各インスタンスの順序によって列インデックスが決まります。モデルのComponent::completed() シグナルが発信されると、カラムとロールが確立され、モデルの寿命まで固定されます。

サポートされる行のデータ構造

各行はツリーのノードを表します。各ノードは同じタイプの列を持ちます。TreeModel は JavaScript/JSON データを扱うように設計されているため、各行は単純なキーと値のペアのリストとなります:

                {
                    checked: false,
                    size: "—",
                    type: "folder",
                    name: "Pictures",
                    lastModified: "2025-05-30",
                    rows: [{
                            checked: true,
                            size: "3.5 MB",
                            type: "file",
                            name: "Vacation.jpg",
                            lastModified: "2025-05-15",
                        }, {
                            checked: false,
                            size: "2.1 MB",
                            type: "file",
                            name: "Family.png",
                            lastModified: "2025-05-20",
                        }]
                }

ノードは子ノードを持つことができ、それらは "rows "キーに関連付けられた配列に格納される。ノードは子ノードを持つことができ、それらは "rows "キーに関連付けられた配列に格納されます。"rows "はこの目的のために予約されています。子ノードのリストのみがこのキーに関連付けられます。

モデルの操作は、QModelIndices 。特定の行/ノードにアクセスするには、getRow() 関数を使用します。rows プロパティを使ってモデルのJavaScriptデータに直接アクセスすることも可能ですが、この方法でモデルデータを変更することはできません。

新しい行を追加するには、appendRow ()を使用します。既存の行を変更するには、setRow()、removeRow()、clear() を使用します。

プロパティの説明

columnCount : int [read-only]

この読み取り専用プロパティは、モデルの列数を保持します。

列数は、rows プロパティが設定されるか、appendRow() が初めて呼び出された後、モデルの存続期間中固定されます。


rows : object

このプロパティは、行の配列の形式でモデル・データを保持します。

getRow(),setRow(),appendRow(),clear(),columnCountも参照


メソッド・ドキュメント

appendRow(object treeRow)

treeRow をルート・ノードに追加します。

setRow() およびremoveRow() も参照して ください。


appendRow(QModelIndex parent, object treeRow)

treeRow の値(セル)を持つ新しい treeRow をparent に追加します。

treeModel.appendRow(index, {
    checked: false,
    size: "-",
    type: "folder",
    name: "Orders",
    lastModified: "2025-07-02",
    rows: [
        {
            checked: true,
            size: "38 KB",
            type: "file",
            name: "monitors.xlsx",
            lastModified: "2025-07-02"
        },
        {
            checked: true,
            size: "54 KB",
            type: "file",
            name: "notebooks.xlsx",
            lastModified: "2025-07-02"
        }
    ]
});

parent が無効な場合、treeRow がルート・ノードに追加されます。

setRow() およびremoveRow()も参照してください


clear()

モデルからすべての行を削除します。

removeRow() も参照して ください。


variant data(QModelIndex index, string role)

指定されたrole に属する、指定されたindexTreeModel のデータを返します。

index() およびsetData() も参照して ください。


object getRow(const QModelIndex &rowIndex)

モデル内のrowIndex にある treeRow を返します。

代わりに setTreeRow() を使用してください。

代わりに setTreeRow() を使用してください。setRow()、appendRow()、removeRow()も参照して ください。


QModelIndex index(list<int> treeIndex, int column)

指定されたtreeIndexcolumn を参照するQModelIndex オブジェクトを返します。このオブジェクトをdata() 関数に渡すと、そのセルのデータを取得でき、setData() に渡すと、そのセルの内容を編集できます。

最初のパラメータtreeIndex は、ルートから目的の行までたどる行番号のパスを表し、ツリー内のナビゲー ションに使用される。これは、例を通して説明するのが一番わかりやすい。

  • ツリーのルートは特別であり、無効なQModelIndex
  • ノードAはルートの最初の子であり、対応するtreeIndex[0] である。
  • ノードBはノードAの最初の子である。AのtreeIndex[0] なので、BのtreeIndex[0,0] となる。
  • ノードCはAの2番目の子であり、そのtreeIndex[0,1] である。
  • ノードDはAの3番目の子で、そのtreeIndex[0,2]
  • ノードEはルートの2番目の子で、そのtreeIndex[1]
  • ノードFはルートの3番目の子で、そのtreeIndex[2]

このオーバーロードを使用すると、親ノードへのQModelIndex を持たずに、ノードへのQModelIndex を得ることができる。

指定されたリストでノードが見つからない場合、無効なモデルインデックスが返されます。無効なモデル・インデックスはノードのルートを参照していることに注意してください。

QMLのQModelIndexと関連するクラスdata()も参照してください


QModelIndex index(int row, int column, object parent)

与えられたparentrowcolumn を参照するQModelIndex オブジェクトを返します。このオブジェクトをdata() 関数に渡すことで、そのセルからデータを取得したり、setData() に渡すことで、そのセルの内容を編集したりすることができます。

QML の QModelIndex と関連するクラス、およびdata()も参照してください


removeRow(QModelIndex rowIndex)

rowIndex によって参照される TreeRow をモデルから削除します。

treeModel.removeTreeRow(rowIndex)

clear() も参照して ください。


bool setData(QModelIndex index, variant value, string role)

指定されたindex の TreeRow にrole で指定されたデータフィールドをvalue で挿入または更新します。成功した場合は true、失敗した場合は false を返します。

data() およびindex()も参照して ください。


setRow(QModelIndex rowIndex, object treeRow)

モデルのrowIndex にある TreeRow をtreeRow で置き換えます。子行がある行は拒否されます。

すべての列/セルがtreeRow に存在し、正しい順序でなければなりません。その行の子行は影響を受けません。

treeModel.setRow(rowIndex, {
    checked: true,
    size: "-",
    type: "folder",
    name: "Subtitles",
    lastModified: "2025-07-07",
    iconColor: "blue"
});

appendRow()も参照してください


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.