Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. QML Javascript Databse Process
Qt 6.11 is out! See what's new in the release blog

QML Javascript Databse Process

Scheduled Pinned Locked Moved Qt in Education
3 Posts 1 Posters 5.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jinex2011
    wrote on last edited by
    #1

    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")
                                     
                             }
                        }
                   }
          }
     }
    

    }
    @

    "Nothing like the power of programming!"

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jinex2011
      wrote on last edited by
      #2

      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'.

      "Nothing like the power of programming!"

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jinex2011
        wrote on last edited by
        #3

        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;
        }
        

        }
        @

        "Nothing like the power of programming!"

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved