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

SE1 ikruis at gmail.com
Mon Dec 28 13:04:25 UTC 2009



Arno Rehn-3 wrote:
> 
> 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
> _______________________________________________
> Kde-bindings mailing list
> Kde-bindings at kde.org
> https://mail.kde.org/mailman/listinfo/kde-bindings
> 
> 

That should get me started, thank you! I've got the latest qyoto compiled
and running on Windows XP now (without even touching linux! haven't tested
it very much yet though). The new generator is great! Only issue I had with
it is that it doesn't complain when it can't find #include's, I've been
searching around for hours why it wouldn't work, only to find out I had the
include dirs messed up ;). Another issue I encountered when running Qyoto is
that the FindMethodId function didn't get marshalled properly. I've been
able to track it down to the fact that it returns a structure which messes
things up. I don't know if it's just a MS.NET quirk but I couldn't get the
function to be called properly (the mangled name ended up in the classname,
signature in the mangled name and junk in the signature). I've hacked around
it by simply passing Smoke::ModuleId by reference into the function, see the
attached diff.

http://old.nabble.com/file/p26942828/patch_findmethodid_win32.diff
patch_findmethodid_win32.diff 

Ilmar Kruis
-- 
View this message in context: http://old.nabble.com/Combining-C%2B%2B-Qt-with-Qyoto-tp26894862p26942828.html
Sent from the kde-bindings mailing list archive at Nabble.com.




More information about the Kde-bindings mailing list