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.
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.
First way.
Create 'script' directory near your executable file.
Create 'script/org' subdirectory.
Create 'script/org/__init__.js' file:
Create 'script/org/mylib' subdirectory.Qt Code:
__setupPackage__(__extension__);To copy to clipboard, switch view to plain text mode
Create 'script/org/mylib' file:
Qt Code:
__setupPackage__(__extension__); org.mylib.myAddition = function(a, b) { return a + b; }To copy to clipboard, switch view to plain text mode
You have created an extension 'org.mylib' that provides a function myAddition.
Qt Code:
QScriptEngine::importExtension('org.mylib'); // print QVariant(int, 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.
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
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:
__setupPackage__("math"); math.add = function(a, b) { return a+b; }To copy to clipboard, switch view to plain text mode
Closed all files, compiled the whole thing from scratch, but
Qt Code:
eng->importExtension("math");To copy to clipboard, switch view to plain text mode
complains that there is no "math"-Extension available. Any advice?
May be create 'script' directory in your 'Extensions', and work only in 'script'.
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.
Yes, it's necessary to a "script" directory. This string hard-coded in qt.
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.
Bookmarks