0

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.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
cholf
  • 9
  • 4

1 Answers1

2

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.

Kevin Krammer
  • 5,159
  • 2
  • 9
  • 22