Unsolved Displaying JavaScript variable in QML
-
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. -
@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
-
-
@gabor53
you should dodb.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)