PDA

View Full Version : Accessing array in both C++ and QtScript



abernat
2nd September 2009, 17:19
I have an array of floats that I'd like to be able to access from QtScript. My initial thought was to use QVector, as I can get a pointer to the data to treat it as an array (which is necessary for some 3rd party software I'm interfacing with), but the script bridge only seems to support QLists, not QVectors. Any suggestions how to make this scenario possible? I'd hate to have to draw a penalty for constantly recreating the array in C++ every time a script needs to change its contents.

JohannesMunk
2nd September 2009, 18:16
Hi!

There are several possibilities.


Simply make QVector a known sequence type:

qScriptRegisterSequenceMetaType<QVector<double> >(engine);
Then you can call a function from scripting that returns a QVector.
-- Before you can call ScriptRegister, you need to declare the QVector<double> metatype, first: Q_DECLARE_METATYPE(QVector<double>)

The most elaborate possibility would be to subclass a QScriptClass as is shown for a ByteArray in the "Custom Script Class Example" in the documentation. This will also give you full Integration - use of [], Length ..

Just use access functions get(i) and set(i)..


Cheers

Joh

abernat
2nd September 2009, 19:06
That's great, thanks. Would this also make it available to Javascript in WebKit? There's a possibility I'd like to do the same thing there.

JohannesMunk
2nd September 2009, 20:01
I have no idea. Anybody else?

Johannes

wysota
2nd September 2009, 21:20
That's great, thanks. Would this also make it available to Javascript in WebKit?

No, they are two completely distinct engines.

abernat
4th September 2009, 19:54
No, they are two completely distinct engines.

Any ideas, wysota, on how to accomplish something similar for WebKit? Thanks.

wysota
4th September 2009, 19:57
I don't think you can. At least not currently. What do you need it for? Can't you use regular javascript arrays?

abernat
4th September 2009, 21:16
I'd use a regular Javascript array, but I'm not sure what the best method would be to get it to the C++ side. The application is using sampled data, so an array of floats makes the most sense. I'd like to build a tutorial module for the application, but I don't have a whole lot of time to spend on it, so I'd really like to use WebKit and just develop some HTML as the interface for the tutorial. That means either have the sample data created on the C++ side and exposed to Javascript or creating it in Javascript and exposing it to C++. I don't really have a preference either way. The problem is figuring out the correct (best?) way to share data between the two different sides of the application. If I can just share the Javascript array, that's great, but I've been unable to figure out how to do it thus far.

wysota
4th September 2009, 22:22
I'd use a regular Javascript array, but I'm not sure what the best method would be to get it to the C++ side.
Well... you can't access WebKit's "memory space" from C++ (yet!) so this is really meaningless.


That means either have the sample data created on the C++ side and exposed to Javascript or creating it in Javascript and exposing it to C++. I don't really have a preference either way. The problem is figuring out the correct (best?) way to share data between the two different sides of the application. If I can just share the Javascript array, that's great, but I've been unable to figure out how to do it thus far.

What do you need javascript for? Maybe importing the web technology is more of a burden than real help?

You can use QtScript in C++ code and Qt is quite good in providing visual interface to the logic behind the application.

abernat
6th September 2009, 21:25
No doubt that Qt's great for building interfaces (I do it all the time), but for this particular usage of WebKit could have been a great fit. By having the tutorials themselves be on an intranet as web pages, I'd be free to update them as time permits without having to worry about a new release of the software -- just update the HTML and Javascript on the servers. At the same time, I'd be able to allow the users to provide their own sample data for use in the tutorial. There are still ways to do it, but it would have been nice for the Javascript to have direct access to the float arrays I'm using in the application for display in HTML.

It's not a showstopper or anything, it would have just been a nice solution for this particular component.

wysota
6th September 2009, 22:21
By having the tutorials themselves be on an intranet as web pages, I'd be free to update them as time permits without having to worry about a new release of the software -- just update the HTML and Javascript on the servers.
That's really not an argument as you can download files from a webserver without WebKit as well. And users can still manipulate the environment using QtScript.