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. Function doesn't work from imported js-file, but work from within qml-file
Forum Updated to NodeBB v4.3 + New Features

Function doesn't work from imported js-file, but work from within qml-file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 836 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.
  • K Offline
    K Offline
    Kyle G.
    wrote on last edited by
    #1

    Here is a function from within main.qml and it work as expected:

    ...
    TextField {
                id: textField1
                placeholderText: qsTr("Password")
                focus: true 
    }
    
    Button {
                id: button1
                text: qsTr("Press Me")
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.topMargin: 90
                onClicked: DB.addPassword(textField1.text)
            }
    }
    
    function addPassword(pass) {
        print("DB.addPassword(): " + pass)
        var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000)
     
        db.transaction(
                    function(tx) {
                        tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)');
                        tx.executeSql('DELETE from passwords');
                        tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]);
    
                        var rs = tx.executeSql('SELECT * FROM passwords')
                        var r = rs.rows.item(0).pw
                        console.log("addPassword: " + r)
    })
    

    But if I move it to separate file, db.js it doesn't work anymore

    function addPassword(pass) {
        print("DB.addPassword(): " + pass)
        db.transaction(
                    function(tx) {
                        tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)');
                        tx.executeSql('DELETE from passwords');
                        tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]);
    
                        var rs = tx.executeSql('SELECT * FROM passwords')
                        var r = rs.rows.item(0).pw
                        console.log("addPassword: " + r)
                    })
    }
    

    console.log("addPassword: " + r) tell me something "undefinded":

    qml: DB.addPassword(): testtesttest
    qml: addPassword: undefined
    

    What I'm doing wrong?
    Thanks!

    1 Reply Last reply
    0
    • zenghsingZ Offline
      zenghsingZ Offline
      zenghsing
      wrote on last edited by
      #2

      Do you include js file correctly?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kyle G.
        wrote on last edited by
        #3

        Here both files.
        main.qml:

        import QtQuick 2.7
        import QtQuick.Controls 2.0
        import "DB.js" as DB
        //import QtQuick.LocalStorage 2.0
        
        ApplicationWindow {
            visible: true
            width: 640
            height: 480
            title: qsTr("PassCheck")
        
            Column {
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.topMargin: 70
                anchors.top: parent.top
                spacing: 50
        
                TextField {
                    id: textField1
                    placeholderText: qsTr("Password")
                    focus: true
                }
        
                Button {
                    id: button1
                    text: qsTr("Press Me")
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.topMargin: 90
                    onClicked: DB.addPassword(textField1.text)
                }
            }
        
        /*	function addPassword(pass) {
                print("DB.addPassword: " + pass)
                var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000)
                db.transaction(
                            function(tx) {
                                tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)');
                                tx.executeSql('DELETE from passwords');
                                tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]);
        
                                var rs = tx.executeSql('SELECT * FROM passwords')
                                var r = rs.rows.item(0).pw
                                console.log("R: " + r)
                            })
            } */
            Component.onCompleted: DB.openDB()
        }
        

        and DB.js

        .pragma library
        .import QtQuick.LocalStorage 2.0 as Sql
        
        var db
        
        function openDB() {
            print("DB.openDB()")
            db = Sql.LocalStorage.openDatabaseSync("CheckPassDB","1.0","The CheckPass Database", 1000)
            createPassTable();
        }
        
        function createPassTable() {
            print("DB.createPassTable()")
            db.transaction( function(tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)');
            })
        }
        
        function addPassword(pass) {
            print("DB.addPassword(): " + pass)
            db.transaction(
                        function(tx) {
                            tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)');
                            tx.executeSql('DELETE from passwords');
                            tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]);
        
                            var rs = tx.executeSql('SELECT * FROM passwords')
                            var r = rs.rows.item(0).pw
                            console.log("addPassword: " + r)
                        })
        }
        
        
        1 Reply Last reply
        0
        • zenghsingZ Offline
          zenghsingZ Offline
          zenghsing
          wrote on last edited by
          #4

          Does your DB.js and main.qml are in the same folder?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kyle G.
            wrote on last edited by
            #5

            Yep, they're in the same folder

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kyle G.
              wrote on last edited by
              #6

              Problem was solved by removing ~/.local/share/checkpass catalog (Linux)

              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