Results 1 to 9 of 9

Thread: QScriptEngine::importExtension()

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QScriptEngine::importExtension()

    Hello All,
    I have read the describe about QScriptEngine::importExtension() and the artical "Creating QtScript Extensions",but I have not found out how to use this function. Anyone can give me a sample. Thanks.

  2. #2
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptEngine::importExtension()

    First way.
    Create 'script' directory near your executable file.
    Create 'script/org' subdirectory.
    Create 'script/org/__init__.js' file:
    Qt Code:
    1. __setupPackage__(__extension__);
    To copy to clipboard, switch view to plain text mode 
    Create 'script/org/mylib' subdirectory.
    Create 'script/org/mylib' file:
    Qt Code:
    1. __setupPackage__(__extension__);
    2. org.mylib.myAddition = function(a, b) {
    3. return a + b;
    4. }
    To copy to clipboard, switch view to plain text mode 

    You have created an extension 'org.mylib' that provides a function myAddition.
    Qt Code:
    1. QScriptEngine::importExtension('org.mylib');
    2. // print QVariant(int, 3);
    3. qDebug() << QScriptEngine::globalObject()::property('org').property('mylib').property('myAddition').call(QScriptEngine::globalObject(), QScriptValueList() << 1 << 2).toVariant();
    To copy to clipboard, switch view to plain text mode 

    For second way, see Qt Script Generator, which generate qt script plugin extensions for all qt classes.

  3. #3
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScriptEngine::importExtension()

    Hi,asvil
    Thanks very much for you reply .I have try your ways .
    but I still have some questions.
    1. Can I change the script files path,not only put them in the script near the executable file?
    2. The artical "creating qtscript extention" introduces that "QScriptEngine will look for a QScriptExtensionPlugin that provides the relevant extension by querying each plugin for its keys() until a match is found. The plugin's initialize() function will be called after the relevant __init__.js (if any) has been evaluated."
    If I create a QScriptExtensionPlugin class , whether I import the plugin only can by the way of static extensions.
    Last edited by wookoon; 25th July 2010 at 20:38. Reason: updated contents

  4. #4
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptEngine::importExtension()

    Quote Originally Posted by wookoon View Post
    Hi,asvil
    Thanks very much for you reply .I have try your ways .
    but I still have some questions.
    1. Can I change the script files path,not only put them in the script near the executable file?
    Yes, QCoreApplication::addLibraryPath();
    Quote Originally Posted by wookoon View Post
    ......If I create a QScriptExtensionPlugin class , whether I import the plugin only can by the way of static extensions.
    What do you mean? Link qt plugins statically? QScriptExtensionPlugin class may be linked statically or dynamically. It depends on your project architecture.

  5. #5
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptEngine::importExtension()

    Hello!

    I have registered just today, since I also have a problem with this. I am developing on Linux with 4.6. I have followed the above steps carefully, double checked these steps:

    -> created a directory in the same path as the executable is in called "Extensions"
    -> added the path including Extensions to libraryPaths() of my QApplication instance, confirmed that by calling libraryPaths()
    -> created a directory "math" in "Extensions"
    -> created a file __init__js in "math"
    -> put the following code into __init__.js:
    Qt Code:
    1. __setupPackage__("math");
    2.  
    3. math.add = function(a, b)
    4. {
    5. return a+b;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Closed all files, compiled the whole thing from scratch, but

    Qt Code:
    1. eng->importExtension("math");
    To copy to clipboard, switch view to plain text mode 

    complains that there is no "math"-Extension available. Any advice?

  6. #6
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptEngine::importExtension()

    May be create 'script' directory in your 'Extensions', and work only in 'script'.

  7. #7
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptEngine::importExtension()

    Wow, thank you very much for the great answer, works like a charm now. Is it necessary to a "script" directory? It's not a big issue, just some "make-up", because all files in the project's folder are starting with a capital.

  8. #8
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptEngine::importExtension()

    Yes, it's necessary to a "script" directory. This string hard-coded in qt.

  9. #9

    Default Re: QScriptEngine::importExtension()

    I can't seem to get namespaces to work at all. It actually throws a parse error when I try to make a namespace. I can only seem to create functions and variables in global space.

    This is from a different post, but it has a simple example of mine I haven't been able to get working.

    http://www.qtcentre.org/threads/4307...ght=namespaces

    Does anyone see a problem with what I'm doing? It seems exactly like the examples in previous replies.

Similar Threads

  1. how to use importExtension
    By wookoon in forum Qt Programming
    Replies: 0
    Last Post: 23rd July 2010, 08:49
  2. QScriptEngine
    By coderbob in forum Qt Programming
    Replies: 10
    Last Post: 25th January 2010, 16:12
  3. QScriptEngine in Qt 4.6.0
    By Fastman in forum Qt Programming
    Replies: 0
    Last Post: 25th December 2009, 21:00
  4. QTextCursor and QScriptEngine
    By roxton in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2008, 19:47
  5. Interpretting javascript using QScriptEngine
    By Saranakumardb in forum Qt Programming
    Replies: 8
    Last Post: 31st July 2008, 18:56

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.