0
votes

I have the following code. I need to set different "model" field for different TableViews. How can i do this?

I need override "model" field for every TableView. It is possible?

// main.qml
ApplicationWindow {
    id: window

    TabView {
         Tab {
             title: "Tab 1"
             MyTable {}
         }
         Tab {
             title: "Tab 2"
             MyTable {}
         }
     }
}

.

// MyTable.qml
MyTableView {
    TableView {
        TableViewColumn {
            role: "number"
            title: "Number"
        }
        model: MyModel
    }
}

The next code is correct but too long.

// main.qml
ApplicationWindow {
    id: window
    TabView {
         Tab {
             title: "Tab 1"
             TableView {
                 TableViewColumn {
                     role: "number"
                     title: "Number"
                 }
                 model: MyModel_1
             }
         }
         Tab {
             title: "Tab 2"
             TableView {
                 TableViewColumn {
                     role: "number"
                     title: "Number"
                 }
                 model: MyModel_2
             }
         }
     }
}
1
You should clarify your question if you expect for the answer.folibis
@dzukp Are you implying that you want a different model for each MyTable? So in main.qml, every time you create an instance of MyTable {}, the model can be changed?TrebledJ
@John Law you are right. I want change model for each MyTable {}.dzukp

1 Answers

1
votes

You can set a property alias for the model. First set an id for the TableView then hook up its model.

// MyTable.qml
MyTableView {
    property alias model: tableView.model

    TableView {
        id: tableView
        TableViewColumn {
            role: "number"
            title: "Number"
        }
        // model: MyModel // redundant in most cases
                          // unless you want to have a DEFAULT model
    }
}

And use model in each MyTable item like so:

// main.qml
ApplicationWindow {
    id: window

    TabView {
         Tab {
             title: "Tab 1"
             MyTable { model: MyFirstModel }
         }
         Tab {
             title: "Tab 2"
             MyTable { model: MySecondModel }
         }
     }
}