Results 1 to 3 of 3

Thread: QML file not loading external JS file?

  1. #1
    Join Date
    Apr 2010
    Posts
    3
    Platforms
    Windows Symbian S60

    Default QML file not loading external JS file?

    Hello,

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

    Test.qml
    Qt Code:
    1. import Qt 4.6
    2. import "a.js" as Misc
    3.  
    4.  
    5. Rectangle {
    6. width: 200
    7. height: 500
    8.  
    9. Text {
    10. text: Misc.doubleFloat(2.5)
    11. anchors.right: parent.right
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 


    a.js
    Qt Code:
    1. function doubleFloat(a) {
    2. a = parseFloat(a);
    3. return a*2;
    4. }
    To copy to clipboard, switch view to plain text mode 


    Application Ouput panel shows

    Qt Code:
    1. TypeError: Result of expression 'Misc.doubleFloat' [undefined] is not a function.
    To copy to clipboard, switch view to plain text mode 

    The function works fine if I inline it and remove the qualifier:

    Test.qml
    Qt Code:
    1. import Qt 4.6
    2.  
    3. Rectangle {
    4. width: 200
    5. height: 500
    6.  
    7. function doubleFloat(a) {
    8. a = parseFloat(a);
    9. return a*2;
    10. }
    11.  
    12. Text {
    13. text: doubleFloat(2.5)
    14. anchors.right: parent.right
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by anothertest; 13th April 2010 at 12:51. Reason: change subscription options

  2. #2
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QML file not loading external JS file?

    Did you load the script?
    ie. in your QML do you have
    Qt Code:
    1. Script { source: "yourScript.js" }
    To copy to clipboard, switch view to plain text mode 
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  3. #3
    Join Date
    Apr 2010
    Posts
    3
    Platforms
    Windows Symbian S60

    Default Re: QML file not loading external JS file?

    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...avascript.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?
    Last edited by anothertest; 14th April 2010 at 09:17. Reason: updated contents

Similar Threads

  1. Open an external file from qt
    By maider in forum Qt Programming
    Replies: 9
    Last Post: 12th March 2010, 10:09
  2. Using qDebug to write to an external file
    By qtUser500 in forum Newbie
    Replies: 4
    Last Post: 18th November 2009, 14:01
  3. External Javascript file not loading initially
    By bkudrle in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2009, 17:05
  4. OpenGL file loading formats
    By TheKedge in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2008, 15:44
  5. Database: How to use an external file?
    By goes2bob in forum Newbie
    Replies: 10
    Last Post: 23rd January 2008, 14:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.