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. Return var from js file to qml.
Forum Update on Monday, May 27th 2025

Return var from js file to qml.

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.4k Views
  • 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 10 Jun 2013, 21:58 last edited by
    #1

    I'm trying to return a js var to my main qml but I'm getting this error.
    @Error: Cannot assign [undefined] to QString@

    here is my js code.
    @var index_count = 0
    var up_check = 0
    function get_db(a, up_check) {

    var db = LocalStorage.openDatabaseSync("DB5", "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<1000; z++){
    tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'Movie', z ]);
    }
    */
    var rs = tx.executeSql('SELECT * FROM Greeting');
    var r = ""
    for(var i = 0; i < a; i++) {
    if (i>index_count-1 && up_check ===0)
    r += rs.rows.item(i).salutation + ": " + rs.rows.item(i).salutee + "\t\t"

                     if (up_check === 1){
                         var size_check =0
                         if (i >index_count-11   && size_check !==9)  {
                           r += rs.rows.item(i).salutation + ": " + rs.rows.item(i).salutee + "\t\t"
                          size_check++
                         }
                      }
                  }           
           console.log(r);
           index_count=a;             
           return r;
        })}@
    

    and my main qml mousearea in which I'm trying get the var r

    @
    ...
    property string globalForJs: "It's";
    ...
    ..

    MouseArea {id: movie_mouse_mm; x: 394; y: 221; width: 104; height: 100;
    onClicked:{
    film_image.state = "rotated"
    playbanner.start()
    up_down.start()
    globalForJs=Logic.get_db(5,0)
    console.log(globalForJs)}
    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Alek Śmierciak
      wrote on 11 Jun 2013, 10:49 last edited by
      #2

      Hello,

      you have some nested function definitions but you don't actually call them, hence the return is undefined.

      Try moving the inner function definition aside and calling it as an argument to the transaction() function instead. You will have to provide a variable to assign to as well, since currently you do nothing with the returned variable in the outer function.

      1 Reply Last reply
      0

      1/2

      10 Jun 2013, 21:58

      • Login

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