1

I'm writing an app for my n9 and have a database problem. I'm not using the main.cpp or other c++ file for the app. I create,delete,add etc. data to a database using javscript from within the qml. Right now I'm simply putting out a string with all entries. Everything works fine. But now I want to show the entries in the database as a ListView. How can I do that?

John Smith
  • 19
  • 1

1 Answers1

1

You can create a model for your ListView dynamically. Something like these:

import QtQuick 2.0
import "main.js" as Main

Rectangle {
    id: root

    ListView {
         width: 180; height: 200

         model: Main.createModel(root)
         delegate: Text {
             text: name + ": " + number
        }
     }
}    

and main.js

function createModel(parent) {
    var s = 'import QtQuick 2.0; ListModel {\n';
    var data = ["a", "b"];  // your data from database will be here
    for(var x in data) {
        var s2 = "ListElement {name: \"" + x+ "\"; number: \"" + x + "\" }\n";
        s += s2;
    }

    s += "}\n";
    console.log(s);
    return Qt.createQmlObject(s, parent, "mainModel");
}
Kakadu
  • 2,837
  • 19
  • 29