PDA

View Full Version : QML file not loading external JS file?



anothertest
13th April 2010, 10:46
Hello,

I've created a simple QML file which imports an external JavaScript file:

Test.qml

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

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


Application Ouput panel shows


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

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 locating it. I have also tried providing the absolute path in the import statement. I still receive the same error.

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

Archimedes
13th April 2010, 23:29
Did you load the script?
ie. in your QML do you have

Script { source: "yourScript.js" }

anothertest
14th April 2010, 09:11
Trolltech's tutorial for 4.7 snapshot on integrating JavaScript states the import statement should be used to load external JavaScript files:

http://doc.qt.nokia.com/4.7-snapshot/qdeclarativejavascript.html

The script { } method was superseded by the import method. However the code you suggested does work, so my next question is, do you know when it was superseded?