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() 신호가 전송되면 열과 역할이 설정된 후 모델의 수명 기간 동안 고정됩니다.
지원되는 행 데이터 구조
각 행은 트리의 노드를 나타냅니다. 각 노드에는 동일한 유형의 열이 있습니다. 트리모델은 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" 키는 이 용도로만 사용되며, 하위 노드 목록만 이 키와 연결되어야 합니다.
모델은 QModelIndices 을 통해 조작됩니다. 특정 행/노드에 액세스하려면 getRow() 함수를 사용할 수 있습니다. rows 속성을 통해 모델의 JavaScript 데이터에 직접 액세스할 수도 있지만 이 방법으로 모델 데이터를 수정할 수는 없습니다.
새 행을 추가하려면 appendRow()를 사용합니다. 기존 행을 수정하려면 setRow(), removeRow() 및 clear()를 사용합니다.
속성 문서
columnCount : int |
rows : object |
이 속성은 모델 데이터를 행 배열 형태로 보유합니다.
getRow(), setRow(), appendRow(), clear() 및 columnCount도 참조하세요 .
메서드 문서
appendRow(QModelIndex parent, object treeRow) |
treeRow 에 있는 값(셀)을 사용하여 parent 에 새 treeRow를 추가합니다.
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 및 data()의 QModelIndex 및 관련 클래스도참조하세요 .
주어진 row 및 column 의 주어진 parent 을 참조하는 QModelIndex 객체를 반환하며, data() 함수에 전달하여 해당 셀에서 데이터를 가져오거나 setData() 에 전달하여 해당 셀의 내용을 편집할 수 있습니다.
QML 및 data()의 QModelIndex 및 관련 클래스도참조하세요 .
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.