TreeModel QML Type
単純なツリーモデルをカプセル化します。詳細...
Import Statement: | import Qt.labs.qmlmodels |
Since: | Qt 6.10 |
プロパティ
- columnCount : int
- rows : object
方法
- 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 |
rows : object |
このプロパティは、行の配列の形式でモデル・データを保持します。
getRow(),setRow(),appendRow(),clear(),columnCountも参照 。
メソッド・ドキュメント
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 がルート・ノードに追加されます。
clear() |
モデルからすべての行を削除します。
removeRow() も参照して ください。
object getRow(const QModelIndex &rowIndex) |
指定されたtreeIndex とcolumn を参照するQModelIndex オブジェクトを返します。このオブジェクトをdata() 関数に渡すと、そのセルのデータを取得でき、setData() に渡すと、そのセルの内容を編集できます。
最初のパラメータtreeIndex は、ルートから目的の行までたどる行番号のパスを表し、ツリー内のナビゲー ションに使用される。これは、例を通して説明するのが一番わかりやすい。
|
このオーバーロードを使用すると、親ノードへのQModelIndex を持たずに、ノードへのQModelIndex を得ることができる。
指定されたリストでノードが見つからない場合、無効なモデルインデックスが返されます。無効なモデル・インデックスはノードのルートを参照していることに注意してください。
QMLのQModelIndexと関連するクラス、data()も参照してください 。
与えられたparent のrow とcolumn を参照するQModelIndex オブジェクトを返します。このオブジェクトをdata() 関数に渡すことで、そのセルからデータを取得したり、setData() に渡すことで、そのセルの内容を編集したりすることができます。
QML の QModelIndex と関連するクラス、およびdata()も参照してください 。
removeRow(QModelIndex rowIndex) |
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.