0
votes

The reference demo is like this:

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

I want to change the fileSystemModel to my self-defined model. How I should do this? Thanks.

1

1 Answers

2
votes

You can export your model the same way the example is exporting "fileSystemModel".

Basically the steps are

  1. Create an instance of your model
  2. Set your instance as a context property on the engine's root context
  3. Load main QML file

Example assuming a locally defined QQuickView view but QQuickWindow or QQmlApplicationEngine would result in very similar code:

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

The first argument of setContextProperty() is the name that is visible on the QML side, i.e. it works like the value if an "id" property in QML.