marmistrz
marmistrz

Reputation: 6392

Best way to store list contents in QML

My app modifies the user agent string on a mobile platform. (User Agent Tool)

It'd would be nice though to let the users customize their preset list. I'm wondering which way to store the contents would be the best.

Operations needed: loading the list, adding an element, deleting an element, changing the order of the elements, saving the list.

Thought about using XML as there's XmlListModel available. But is there anything better? If xml is the best, can xml be easily manipulated from the qml side?

Thanks

/edit: my functions are like that:

function getDatabase() {return openDatabaseSync("UserAgentTool", 1.0, "User Agent Tool", 10000); }


function getList()
{
    var db = getDatabase();
    var ret
    db.transaction( function (tx)
    {
        ret = tx.executeSql("SELECT * FROM UserAgents")
    })

    return ret
}

function addToList(ualabel, uastring)
{
    var db = getDatabase();
    db.transaction(function (tx)
    {
        tx.executeSql('CREATE TABLE IF NOT EXISTS UserAgents(ualabel TINYTEXT UNIQUE, uastring TEXT)')
        var ret = tx.executeSql("INSERT OR REPLACE INTO UserAgents VALUES(?, ?)", [ualabel, uastring])
        if (ret.rowsAffected <= 0) sqlError();
    })

}

signal sqlError()

How can I populate a ListView with the selected in getList data?

Thanks

EDIT: This doesn't work:

ListView
{
    anchors.fill: parent
    id: uaview
    model: uamodel
    delegate: Button { text: model.ualabel}
}

Error is: Unable to assign [undefined] to QString text

Upvotes: 0

Views: 1482

Answers (1)

TheBootroo
TheBootroo

Reputation: 7518

With XmlListModel, you won't be able to modify data easily using only QML, but really easy to read. If you need to read write, and want to use only QML, i advise you to use a ListModel as a work model and cache data into LocalStorage (it's currently a SQLite database file with tables, quite classical).

here is the doc of Local Storage

Upvotes: 2

Related Questions