TreeModel QML Type
간단한 트리 모델을 캡슐화합니다. 더 보기...
| Import Statement: | import Qt.labs.qmlmodels |
| Since: | Qt 6.10 |
속성
- columnCount : int
- rows : var
방법
- void appendRow(var treeRow)
- void appendRow(parent, var treeRow)
- void clear()
- variant data(index, string role)
- var getRow(rowIndex)
- QModelIndex index(list<int> treeIndex, int column)
- QModelIndex index(int row, int column, var parent)
- void removeRow(rowIndex)
- bool setData(index, variant value, string role)
- void setRow(rowIndex, var 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()를 사용합니다.
외부 JSON 소스와 함께 트리모델 사용하기
TreeModel의 용도는 QML 파일에서 JSON 데이터를 제자리에 정의하는 것이지만, 그 구조가 TreeModel에 정의된 구조와 일치하는 한 어떤 JSON 객체라도 모델과 함께 사용할 수 있습니다. JSON 개체의 모든 행이 모델에 정의된 열과 일치하는 경우 행 속성을 통해 직접 할당할 수 있습니다.
JSON 객체의 소스는 파일일 수 있습니다. 예를 들어 모델이 다음과 같이 정의된 경우
TreeModel { id: treeModel objectName: "testModel" TableModelColumn { display: "checked" } TableModelColumn { display: "size" } TableModelColumn { display: "type" } TableModelColumn { display: "name" } TableModelColumn { display: "lastModified" } }
로 정의되고 JSON 객체가 파일에 저장되어 있고 모양이
let folders = [
{
"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": "—",
"type": "folder",
"name": "Pictures",
"lastModified": "2025-05-30",
"rows": [
{
"checked": true,
"size": "3.5 MB",
"type": "file",
"name": "Vacation.jpg",
"lastModified": "2025-05-15"
}
]
}
]로 정의된 경우 이 객체는 자바스크립트 파일을 가져와서
모듈
import "TreeData.js" as JsonData
로 가져온 다음 직접 할당할 수 있습니다.
Component.onCompleted: treeView.model.rows = JsonData.folders
경고: JSON 데이터가 신뢰할 수 있는 출처에서 제공되었는지 확인하세요. 모델은 입력을 기반으로 행을 명료하게 채우기 때문에 잘못되거나 신뢰할 수 없는 JSON은 예기치 않은 동작이나 성능 문제를 일으킬 수 있습니다.
JSON 객체를 파싱하여 JavaScript 배열로 할당하는 또 다른 예는 TableModel 을 참조하세요.
TableModelColumn 및 TreeView도 참조하세요 .
속성 문서
columnCount : int [read-only]
이 읽기 전용 속성은 모델의 열 수를 보유합니다.
열 수는 rows 속성이 설정되거나 appendRow()이 처음 호출된 후 모델의 수명 동안 고정됩니다.
rows : var
이 속성은 모델 데이터를 행 배열 형태로 보유합니다.
getRow(), setRow(), appendRow(), clear() 및 columnCount 를참조하세요 .
메서드 문서
void appendRow(var treeRow)
루트 노드에 treeRow 을 추가합니다.
setRow() 및 removeRow()도 참조하세요 .
void appendRow(parent, var 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 는 QModelIndex 에 의해 뒷받침되는 익명 QML 유형입니다. parent 가 유효하지 않으면 treeRow 이 루트 노드에 추가됩니다.
setRow() 및 removeRow()도 참조하세요 .
void clear()
모델에서 모든 행을 제거합니다.
removeRow()도 참조하세요 .
variant data(index, string role)
주어진 index 에서 주어진 role 에 속하는 TreeModel 의 데이터를 반환합니다.
index 은 QModelIndex 에 의해 지원되는 익명 QML 타입입니다.
var getRow(rowIndex)
rowIndex 은 QModelIndex 에 의해 지원되는 익명 QML 유형입니다.
참고: 반환된 객체는 모델의 내용을 수정하는 데 사용할 수 없으므로 대신 setTreeRow()를 사용하세요.
setRow(), appendRow() 및 removeRow()도 참조하세요 .
QModelIndex index(list<int> treeIndex, int column)
주어진 treeIndex 및 column 을 참조하는 객체를 반환하며, data() 함수에 전달하여 해당 셀의 데이터를 가져오거나 setData() 함수에 전달하여 해당 셀의 내용을 편집할 수 있습니다.
반환된 객체는 QModelIndex 으로 뒷받침되는 익명 QML 유형입니다.
첫 번째 매개변수 treeIndex 는 루트에서 원하는 행까지 추적하는 행 번호의 경로를 나타내며 트리 내부 탐색에 사용됩니다. 이는 예시를 통해 가장 잘 설명할 수 있습니다.
|
이 오버로드를 사용하면 부모 노드에 QModelIndex 이 없어도 노드에 QModelIndex 을 얻을 수 있습니다.
지정된 목록에서 노드를 찾을 수 없는 경우 잘못된 모델 인덱스가 반환됩니다. 잘못된 모델 인덱스는 노드의 루트를 참조한다는 점에 유의하세요.
QML 및 data()의 QModelIndex 및 관련 클래스도참조하세요 .
QModelIndex index(int row, int column, var parent)
주어진 parent 의 주어진 row 및 column 을 참조하는 객체를 반환하며, data() 함수에 전달하여 해당 셀에서 데이터를 가져오거나 setData()에 전달하여 해당 셀의 내용을 편집할 수 있습니다.
반환된 객체는 QModelIndex 에 의해 뒷받침되는 익명 QML 유형입니다.
QML 및 data()의 QModelIndex 및 관련 클래스도참조하세요 .
void removeRow(rowIndex)
모델에서 rowIndex 에서 참조하는 TreeRow를 제거합니다. rowIndex 은 QModelIndex 에 의해 지원되는 익명 QML 유형입니다.
treeModel.removeTreeRow(rowIndex)clear()도 참조하세요 .
bool setData(index, variant value, string role)
지정된 index 의 트리행에 role 로 명명된 데이터 필드를 value 로 삽입하거나 업데이트합니다. 성공하면 참을 반환하고 실패하면 거짓을 반환합니다.
index 는 QModelIndex 에 의해 뒷받침되는 익명 QML 타입입니다.
void setRow(rowIndex, var treeRow)
모델에서 rowIndex 의 트리 행을 treeRow 으로 바꾸고 rowIndex 은 익명 QML 유형으로 QModelIndex 으로 되돌립니다.
자식 행이 있는 행은 거부됩니다.
모든 열/셀은 treeRow 에 올바른 순서로 존재해야 합니다. 행의 하위 행은 영향을 받지 않습니다.
treeModel.setRow(rowIndex, { checked: true, size: "-", type: "folder", name: "Subtitles", lastModified: "2025-07-07", iconColor: "blue" });
appendRow()도 참조하세요 .
© 2026 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.