PDA

View Full Version : QScriptEngine::importExtension()



wookoon
25th July 2010, 17:39
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.

asvil
25th July 2010, 20:22
First way.
Create 'script' directory near your executable file.
Create 'script/org' subdirectory.
Create 'script/org/__init__.js' file:


__setupPackage__(__extension__);

Create 'script/org/mylib' subdirectory.
Create 'script/org/mylib' file:


__setupPackage__(__extension__);
org.mylib.myAddition = function(a, b) {
return a + b;
}


You have created an extension 'org.mylib' that provides a function myAddition.


QScriptEngine::importExtension('org.mylib');
// print QVariant(int, 3);
qDebug() << QScriptEngine::globalObject()::property('org').pro perty('mylib').property('myAddition').call(QScript Engine::globalObject(), QScriptValueList() << 1 << 2).toVariant();


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

wookoon
25th July 2010, 21:18
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.

asvil
25th July 2010, 22:59
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();


......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.

leenxkewl
12th October 2010, 21:34
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:


__setupPackage__("math");

math.add = function(a, b)
{
return a+b;
}


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



eng->importExtension("math");


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

asvil
12th October 2010, 21:40
May be create 'script' directory in your 'Extensions', and work only in 'script'.

leenxkewl
12th October 2010, 21:46
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.

asvil
12th October 2010, 22:03
Yes, it's necessary to a "script" directory. This string hard-coded in qt.

strattonbrazil
18th July 2011, 17:33
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/43077-Namespacing-QScriptEngine-extensions?highlight=namespaces

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