QML Javascript Databse Process



  • Hi, I am trying to insert data from my xml file dirrectly into the database but this should be done in the background. Because the application freezes when I am inserting the data.

    @
    import QtQuick 1.0

    Item {
    XmlListModel {
    id: xmlModel
    source: "http://www.example.com/data.xml"
    query: "/feed/"

        XmlRole { name: "ids"; query: "id/string()" }
        XmlRole { name: "overview"; query: "summary/string()"  }
    
    }
    

    ListView {

        model: xmlModel
        //height required for data to load
        height: 1
        delegate: Column {
             Text {
                 function findGreetings() {
                       ++i
                       var db = openDatabaseSync("QDeclarativeExampleDB", "1.0", "The Example QML SQL!", 1000000);
                       db.transaction(
                             function(tx) {
                                    // Create the database if it doesn't already exist
                                       tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)');
    
                                   // Add (another) greeting row
                                      tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ ids, overview ]);
    
                                    }
                                             )
                                 if(i==xmlModel.count)
                                   console.log("done")
                                     
                             }
                        }
                   }
          }
     }
    

    }
    @



  • Okay, I found a solution for background process here: http://developer.qt.nokia.com/wiki/JavaScript_programmer

    But, sending a model is my main problem now because when I send the model the javascript recieves it as 'null'.



  • Ok, this would work for now but if you find a better method please share :)

    this is my WorkerScript JS file "dbprocessor.js"
    @
    var init = function(ids, overview) { // QML is ready
    var db = openDatabaseSync("QDeclarativeExampleDB", "1.0", "The Example QML SQL!", 1000000);
    db.transaction(
    function(tx) {
    // Create the database if it doesn't already exist
    tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)');

                    // Add (another) greeting row
                    tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ ids, overview ]);
                }
                )
    

    }
    WorkerScript.onMessage = function(message) {
    // ... long-running operations and calculations are done here

    switch(message.msg)
    {
    
    case"init":
        WorkerScript.sendMessage({ 'reply': 'Done init'});
        init(message.ids, message.overview);
    
        break;
    }
    

    @

    And my main.qml file

    @import Qt 4.7

    Rectangle {

    property bool done: false
    id: top
    width: 400
    height: 400
    
    property XmlListModel list: XmlListModel { id: xmlModel
        source: "http://www.example.com/data.xml"
        query: "/feed/xml/"
    
    
        XmlRole { name: "ids"; query: "id/string()" }
        XmlRole { name: "overview"; query: "description/string()"  }
    

    }

    WorkerScript {
        id: werk
        source: "dbprocessor.js"
        onMessage: console.log(messageObject.reply)
    }
    
    
    Component {
        id: delegate
        Text { // or whatever item
            MouseArea { // Item click handler
                anchors.fill: parent
                onClicked: {
                        console.log("clicked");
                } // implement the click handler in the JavaScript
            }
            //Just to view data inserted
            text: ids 
            Component.onCompleted:{
                if(!done)
                 {
                    for(var i=0; i< xmlModel.count; i++)
                        werk.sendMessage({'msg': "init", 'ids': xmlModel.get(i).ids,  'overview': xmlModel.get(i).overview  });
                    done = true;
                }
            }
        }
    }
    
    
    ListView {
        anchors.fill:  parent
        model: top.list;
        delegate: delegate;
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.