PDA

View Full Version : QtScript and COM objects



Thor28
5th July 2011, 13:55
Hi. I am trying to use QtScript within a Windows application. I would like the script to work with an existing COM object already instantiated in the application.

I am able to use QAxObject to wrap the COM object. Then I set a global public property to provide a method to access the object. For example,


QAxObject globalCom = new QAxObject( spExistingComObject );
QScriptValue globalObject = globalEngine.globalObject();
QScriptValue objectValue = globalEngine.newQObject( globalCom );
globalObject.setProperty("globalComAlias", objectValue );


This actually works quite well when I reference "normal" properties of the COM object, such as script code like:


globalComAlias.Title = "test title"


However, when the properties are themselves references to COM objects, I get errors like "Reference Error: Title not found". An example script section is:


globalComAlias.SubObject.Title = "test title"


It seems that QtScript does not understand the SubObject. (It seems to be true for anything property that returns an IDispatch*.) The error somewhat makes sense to me-- QtScript doesn't know anything about the subobject. However, unlike the global property for the main object, it isn't clear to me how to provide QtScript the details for the subobject.

I tried a few things like qScriptRegisterMetaType, but either I am doing them incorrectly or that isn't the right angle.

As an aside, the same script works when run under Windows Script, but I would really like to run this under the native QtScript (primarily for the access to the debugger.)

Any thoughts or hints would be appreciated. Thanks.

wysota
5th July 2011, 18:29
You probably need to provide some code that will act as a wrapper around the subobject. QScriptClass is a good place to start. Also have a look at the Default Prototypes example, it shows how to make a class scriptable. There are at least three ways to do it.

In general probably the best overall approach is to provide a complete wrapper over QAxObject. The part that works for you works only because QAxObject exposes its properties as QObject properties. But it won't give you anything defined in QAxBase which I guess is something that is really interesting for you. That's also why accessing subobjects doesn't work -- subobjects are not exposed as Qt properties.