[Kde-bindings] Combining C++ Qt with Qyoto

Arno Rehn arno at arnorehn.de
Wed Dec 23 12:14:17 UTC 2009


On Tuesday 22 December 2009 22:47:54 SE1 wrote:
> Hello,
> 
> maybe you can help me. I've got an existing QGraphicsScene in native code.
> Now I'd like to add items / draw to this from Qyoto. Do you have any tips
>  on how to do this? Is it possible to wrap the QGraphicsScene in C# from a
>  pointer? or is there a way to get native pointers to objects created in
>  C#, so I can pass them to addItem() in native code?
Yes, you can do both. You need to link your native library against 
libqyotoshared.so and libsmokeqt.so (in current trunk versions and later KDE 
4.4 you need libsmokeqtcore.so instead) and include qyoto.h.
You can now either create a C# instance with CreateInstance(), e.g. like this:

Smoke::ModuleIndex mi = Smoke::classMap["QGraphicsScene"];
smokeqyoto_object* to = alloc_smokeqyoto_object(false, mi.smoke, mi.index, 
ptr);
instance = (*CreateInstance)(qyoto_resolve_classname(to), to);

You first get an index to the class you want to wrap and then create a 
smokeqyoto_object. The first parameter to alloc_smokeqyoto_object is about 
whether the underlying C++ instance should be deleted when the C# instance is 
reclaimed by the GC. The next two are the smoke module and the class index and 
the last one is your native pointer to the QGraphicsScene. You create a an 
instance with CreateInstance, which returns a GCHandle (in the IntPtr form). 
Just return this GCHandle pointer from your native function and in C# do the 
following:

IntPtr handle = myNativeMethod();
QGraphicsScene scene = (QGraphicsScene) ((GCHandle) handle).Target;

Now if you already have the C# instance and want to retrieve the underlying 
C++ instance, allocate a GCHandle for the C# instance first and pass that to 
your native method.

GCHandle.Alloc(foo);
myNativeMethod(foo);

in myNativeMethod you now get the smokeqyoto_object from the C# instance with

smokeqyoto_object *o = (smokeqyoto_object*) (*GetSmokeObject)(handle);
(*FreeGCHandle)(handle);
QGraphicsScene *scene = (QGraphicsScene*) o->ptr;

That is pretty much self-explaining I guess. I hope the explanation helped you 
a bit :) Don't hesitate to ask if anything's unclear.

-- 
Arno Rehn
arno at arnorehn.de



More information about the Kde-bindings mailing list