Cimm
Cimm

Reputation: 4775

Add function to individual XmlListModel items

I am new to QML and Qt Quick and was wondering how I can add functions to each individual item in an XmlListModel.

XmlListModel {
  id: books
  source: "Books.xml"
  query: "/books"
  XmlRole { name: "price"; query: "@price/string()" }
}

Say the price is in cents and I want to multiply each price by a factor 100. In another programming language I would add a getPrice() function on the Book class but I don't have access to the individual elements here. I could add it to the XmlListModel with an index parameter but I feel it belongs to the individual book item, no?

Upvotes: 1

Views: 226

Answers (1)

folibis
folibis

Reputation: 12874

In the code above you just defined model and roles to fetch data, not real data. But you can access data itself and data items particularly in delegate, for example:

ListView {
    id: listView
    anchors.fill: parent
    function getPrice(value) {
        return value * 100;
    }
    model: books
    delegate: Row {
        height: 30
        width: parent.width
        Text { text: listView.getPrice(price) }
    }
}

Upvotes: 2

Related Questions