[PyKDE] Need help passing PyQt objects to other C++ code

David Boddie david at boddie.org.uk
Sat Aug 19 12:09:26 BST 2006

On Fri Aug 18 16:43:17 MEST 2006, Jack Jansen wrote:

> I need to pass the underlying C++ object for a given PyQt object to
> another module, but I can't seem to find a way to do this.
> To be a bit more concrete: I have a Python application that creates a
> QWidget, and I would like to pass the C++ equivalent of that QWidget
> to my own C++ code (which is not wrapped with sip but with bgen, a
> different wrapper-generator).

So, you're extending Python rather than embedding it in a C++ application?

> Initially I thought that sip.voidptr was the solution: Cast the
> QWidget to a sip.voidptr, then pass that to my wrapper (either as a
> CObject, or even as an int, if all else fails:-). But: I can't seem
> to find how to get the sip.voidptr for a given QWidget, even though
> that seems to be exactly what sip.voidptr is meant for, according to
> the documentation. Is this possible at all?

Or try importing the sip module and "unwrapping" your widget instance:

  import sip
  addr = sip.unwrapinstance(my_widget)

You'll get a Python integer that can either be passed as it is, or you
can use it to create a voidptr object and obtain a PyCObject from that:

  ptr = sip.voidptr(addr)
  cobj = ptr.ascobject()

> Or am I barking up the wrong tree? I also had a look at the sip API
> for C/C++ code, hoping I could find a method I could pass to
> PyArg_Parse(..., "O&", ...) but no luck. Again, I would even be happy
> with a method that would just return me the bits with no-questions-no-
> guarantees, but I must be looking in the wrong place.

If you can, you should try to do all of this in Python. I think you have
all you need, but let us know if there's something missing.

Good luck!


More information about the PyQt mailing list