Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling a funciton.
Forum Updated to NodeBB v4.3 + New Features

Calling a funciton.

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.7k 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.
  • D Offline
    D Offline
    dstudentx
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckront
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chrisadams
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dstudentx
          wrote on last edited by
          #4

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

          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