how to access data elements from AbstractListModel in qml

216 views Asked by At

https://gist.github.com/eyllanesc/4f47e4f59100340b8328438a39011b31

I used this link to generate a QAbstractList and a SortProxyModel over it. I sorted the list with any one attribute using sortdata method in sortproxymodel class. I also need to access some data from that list for some computations in main.qml. console.log(PersonModel.data(1,'value1')) is the line that I used. Is it wrong?

1

There are 1 answers

0
eyllanesc On BEST ANSWER

If you want to access the information you must pass a QModelIndex and the role:

def data(self, index, role=Qt.DisplayRole):

In your case it should be similar to the following:

mymodel.data(mymodel.index(number_of_row, 0), value_of_role)

For example to the previous .qml I have added modifications and the most important is the following code:

Row{
    id: row2
    height: 40
    anchors.bottom: parent.bottom
    spacing: 100
    ComboBox {
        id: comboBoxRole2
        width: 150
        model: [ "name", "value1", "value2", "value3", "value4"]
    }

    ComboBox {
        id: number
        width: 150
        model: mymodel.rowCount()
    }

    Label{
        id: output
        text: mymodel.data(mymodel.index(number.currentIndex, 0), Qt.UserRole+1 + comboBoxRole2.currentIndex)
    }
}

You can find the complete example in the following link.