Calling a funciton.



  • I'm using this file/function to load and display a database.
    @import QtQuick 2.0
    import QtQuick.LocalStorage 2.0

    Text {
    text: "empty"
    x:350
    y:360

    function get_db(a) {
    var db = LocalStorage.openDatabaseSync("DB2", "1.0", "The Example QML SQL!", 1000000);
    db.transaction(
    function(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)');
    /* for ( var z=0; z<10; z++){
    tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ z, z ]);
    }
    tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'A', 'MOVIE' ]);*/
    var rs = tx.executeSql('SELECT * FROM Greeting');
    var r = ""
    for(var i = 0; i < a; i++) {
    r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\t\t\t"
    }
    text = r
    })}
    //Component.onCompleted: get_db(2)
    }@

    I'd like to able to call this function from a mousearea
    for example
    @get_db(5)@
    from another file, this file being something like this.
    @import "includes"
    ....
    ....
    ....

              MouseArea {
                  id: mousearea1
                  x: 569
                  y: 271
                  width: 141
                  height: 210
                  onClicked:{
                      page.state = 'State1'
                      get_db(5)
                    }
                  }
    

    @

    but get_db(5) doesn't work.



  • Is there any error message ?
    Do you include your .qml file which holds the database logic into the other file ?
    I don't see any reference before get_db(5).
    Try Database.get_db(5) (Database being the name of your .qml file which holds the database logic)



  • The "get_db()" function is a member function of the implicit type defined by your top QML file. To invoke it you need to resolve it on a reference of the instance. There are a few ways you can get access to a reference of the instance: either directly by id (if it's in the same component scope), passing a reference to the object as a parameter of a function, storing a reference in a pragma library JavaScript resource, and so on. An example of the latter is included.

    eg:
    @
    // shared.js
    .pragma library

    var databaseObject = null
    function init(dbObj) {
    databaseObject = dbObj;
    }
    @
    @
    // DatabaseAccess.qml
    import QtQuick 2.0
    import QtQuick.LocalStorage 2.0
    import "shared.js" as Shared
    Text {
    id: root
    ....
    function get_db() {
    ....
    }

    Component.onCompleted: Shared.init(root)
    

    }
    @
    @
    // OtherFile.qml
    import QtQuick 2.0
    import "shared.js" as Shared
    Item {
    Component.onCompleted: {
    Shared.databaseObject.get_db(); // etc.
    }
    }
    @

    I'd suggest reading the documentation about attributes of objects (which includes properties, methods, the id, and so on) for more information about this stuff.

    Cheers,
    Chris.



  • Yes!!! thank you so much for helping me out! you saved me big time


Log in to reply
 

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