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. Displaying JavaScript variable in QML
Forum Updated to NodeBB v4.3 + New Features

Displaying JavaScript variable in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 857 Views 2 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I am using the following code to get a value from an SQLite db:

    
    function getLastID() {
    
        var lastID
        dbInit()
    
        var db = dbGetHandle()
    
        db.transaction(function (tx) {
            var ID = tx.executeSql(
                        'SELECT id FROM IDNumber ORDER BY id DESC LIMIT 1')
    
            for (var i = 0; i < ID.rows.length; i++) {
                lastID = ID.rows.item(i).id
                console.log("lastID: " + lastID)
            }
            return lastID
        })
    }
    

    The QML code to display lastID:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.LocalStorage 2.0
    import QtQuick.Layouts 1.3
    import QtQml.Models 2.3
    import QtQuick.Controls 2.2
    import "Database.js" as JS
    
    Window {
        visible: true
        width: 640
        height: 480
    
        property var newID: JS.getLastID()
    
        Text {
            id: sorszam
            text: newID
        }
    }
    
    

    When I run it, I get the following messages:
    lastID: 1 //this is correct
    file:Counter//main.qml: Unable to assign (undefined) to QString.
    What is the correct way of doing this?
    Thank you for your help.

    raven-worxR 1 Reply Last reply
    0
    • G gabor53

      Hi,
      I am using the following code to get a value from an SQLite db:

      
      function getLastID() {
      
          var lastID
          dbInit()
      
          var db = dbGetHandle()
      
          db.transaction(function (tx) {
              var ID = tx.executeSql(
                          'SELECT id FROM IDNumber ORDER BY id DESC LIMIT 1')
      
              for (var i = 0; i < ID.rows.length; i++) {
                  lastID = ID.rows.item(i).id
                  console.log("lastID: " + lastID)
              }
              return lastID
          })
      }
      

      The QML code to display lastID:

      import QtQuick 2.9
      import QtQuick.Window 2.2
      import QtQuick.LocalStorage 2.0
      import QtQuick.Layouts 1.3
      import QtQml.Models 2.3
      import QtQuick.Controls 2.2
      import "Database.js" as JS
      
      Window {
          visible: true
          width: 640
          height: 480
      
          property var newID: JS.getLastID()
      
          Text {
              id: sorszam
              text: newID
          }
      }
      
      

      When I run it, I get the following messages:
      lastID: 1 //this is correct
      file:Counter//main.qml: Unable to assign (undefined) to QString.
      What is the correct way of doing this?
      Thank you for your help.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @gabor53 said in Displaying JavaScript variable in QML:

      file:Counter//main.qml: Unable to assign (undefined) to QString.

      where is the code of this line?

      Beside that, your getLastID() function does not return a value (undefined). Only your inner function does

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      G 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @gabor53 said in Displaying JavaScript variable in QML:

        file:Counter//main.qml: Unable to assign (undefined) to QString.

        where is the code of this line?

        Beside that, your getLastID() function does not return a value (undefined). Only your inner function does

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Hi @raven-worx ,
        The line is

        text: newID
        

        How can I get the outer function return lastID?
        Thank you.

        raven-worxR 1 Reply Last reply
        0
        • G gabor53

          Hi @raven-worx ,
          The line is

          text: newID
          

          How can I get the outer function return lastID?
          Thank you.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @gabor53
          you should do db.readTransaction(callback(tx)) and return the result object itself or the desired value of it.
          (see https://doc.qt.io/qt-5.6/qtquick-localstorage-qmlmodule.html)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          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