[Qt-qml] Unable to use external JS file with QML

Jack Wootton jackwootton at gmail.com
Tue Apr 13 12:56:29 CEST 2010


Hello,

It was recommended I post this question on qt-qml at trolltech.com after first
posting it on Forum Nokia here:
http://discussion.forum.nokia.com/forum/showthread.php?p=724730

QML documentation states that JavaScript can be integrated with QML:
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativejavascript.html

I've created a simple QML file (Test.qml) which imports an external
JavaScript file (a.js).
*
Test.qml code:*

import Qt 4.6
import "a.js" as Misc

Rectangle {
    width: 200
    height: 500

    Text {
        text: Misc.doubleFloat(2.5)
        anchors.right: parent.right
    }
}


*a.js code:*

function doubleFloat(a) {
         a = parseFloat(a);
         return a*2;
     }



The Application Ouput panel shows the error 'TypeError: Result of expression
'Misc.doubleFloat' [undefined] is not a function'. The function works fine
if I inline it and remove the qualifier:

*Test.qml code:*

import Qt 4.6

Rectangle {
    width: 200
    height: 500

    function doubleFloat(a) {
             a = parseFloat(a);
             return a*2;
         }

    Text {
        text: doubleFloat(2.5)
        anchors.right: parent.right
    }
}

a.js is in the same directory as Test.qml - so there should be no problem
with paths. However, for fun, I tried providing the absolute path in the
import statement. I still receive the same error.

Am I missing something obvious?

Also, without wanting to confuse things, the example code provided in the
document 'Integrating JavaScript' (
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativejavascript.html) shows a
function factorial(a) which uses the function Integer().  However shouldn't
this be parseInt()?

I'm using Qt 4.7 and QtCreator 2.0, both built from source on Windows XP

Many thanks,
Jack
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100413/42115a8f/attachment.html 


More information about the Qt-qml mailing list