2016-09-18 32 views
0

參考演示是這樣的:如何在qml treeview中使用userdefine模型?

TreeView { 
    TableViewColumn { 
     title: "Name" 
     role: "fileName" 
     width: 300 
    } 
    TableViewColumn { 
     title: "Permissions" 
     role: "filePermissions" 
     width: 100 
    } 
    model: fileSystemModel 
} 

我想給fileSystemModel改變我的自定義模式。我應該怎麼做?謝謝。

回答

2

您可以像導出「fileSystemModel」一樣導出模型。

基本步驟是

  1. 創建模型的實例
  2. 設置你的實例作爲發動機的根上下文上下文屬性
  3. 加載主QML文件

示例假設本地定義的QQuickView view但是QQuickWindowQQmlApplicationEngine會導致非常相似的代碼:

MyModel model; 
view.engine()->rootContext()->setContextProperty("_identifierForModel", &model); 

setContextProperty()的第一個參數是在QML一側可見的名稱,即它在QML中的作用類似於「id」屬性的值。