[PyQt] Re: [PyKDE] PyQt 4 doesn't allow to create an application, given an already open display. How to solve it?

Matt Newell newellm at blur.com
Thu Mar 8 18:20:04 GMT 2007


On Thursday 08 March 2007 01:11, Phil Thompson wrote:
> On Thursday 08 March 2007 8:40 am, Matt Newell wrote:
> > On Thursday 08 March 2007 00:07, Ulrich Berning wrote:
> > > mikos at centrum.cz wrote:
> > > >Eh, I have just realized that I have replied directly to Phil
> > > > Thompson, not to this list (this is because I have used unfamiliar
> > > > webmail client). So this is our conversation:
> > > >
> > > >*** Me: ***
> > > >You can look at that example by Zack Rusin, it is very simple, here is
> > > > code: http://ktown.kde.org/~zrusin/examples/argb.tar.bz2 (look at
> > > > main.cpp)
> > > >
> > > >That Display instance come directly from xlib, it is created by
> > > > XOpenDisplay: http://lxr.freedesktop.org/ident?i=XOpenDisplay
> > > >
> > > >In Python, I am using ctypes module to directly access xlib (libX11)
> > > >and call XOpenDisplay.
> > > >
> > > >*** Phil: ***
> > > >I meant, in the context of PyQt. PyQt needs to know about the type.
> > > >
> > > >*** Me: ***
> > > >I can't help you much in this, I don't know much about ctypes, I have
> > > > only learned it because I want to create ARGB windows with PyQt (and
> > > > then I realized that I can't do it because PyQt doesn't support it).
> > > > But you can try this code:
> > > >
> > > >### Python code ###
> > > >import ctypes
> > > >
> > > >class Display(ctypes.Structure):
> > > >pass
> > > >
> > > >xlib = ctypes.CDLL('libX11.so.6')
> > > >xlib.XOpenDisplay.restype = ctypes.POINTER(Display)
> > > >xlib.XOpenDisplay.argtypes = [ctypes.c_char_p]
> > > >
> > > >display = xlib.XOpenDisplay(':0.0')
> > > >### End of Python code ###
> > > >
> > > >This will create 'display' object, which is pointer to 'Display'
> > > > structure (which is derived from standard ctypes.Structure).
> > > >
> > > >But this is all I can tell you, as I said I don't know any details
> > > > about this process, only that it works (I have tried creating window
> > > > with pure xlib).
> > > >
> > > >Ctypes are standard module in python 2.5 (for Python 2.4 it is
> > > > external module), so I think PyQt should support it.
> > >
> > > The ctypes extension doesn't build on AIX, HP-UX, IRIX and Solaris.
> > > With some changes, it may build on IRIX and Solaris, because libffi has
> > > been ported to these platforms. On AIX and HP-UX, you have definitely
> > > lost. I've never understood, why ctypes became a standard module.
> > >
> > > PyQt works on the above platforms. Making it dependent on the ctypes
> > > extension seems to be a bad idea.
> >
> > Display is just a pointer.  I think PyQt could support accepting a ctypes
> > pointer as an arguement without depending on ctypes being available.  The
> > ctypes pointer is simply passed as a PyObject * and regular python api
> > calls are used to check that it is indeed a ctypes pointer, and to get
> > the value.
>
> Ironically ctypes doesn't provide the necessary C API to do this. However
> if (in Python) you can get the address of the real data structure as a
> number (ctypes.addressof() ?) then you can create a sip.voidptr from it and
> I can add the ctors to accept that.
>


import ctypes
import sip

class Display(ctypes.Structure):
	pass

xlib = ctypes.CDLL('libX11.so.6')
xlib.XOpenDisplay.restype = ctypes.POINTER(Display)
xlib.XOpenDisplay.argtypes = [ctypes.c_char_p]

display = xlib.XOpenDisplay(':0.0')

dvp = ctypes.cast(display,ctypes.c_void_p)
print dvp.value
svp = sip.voidptr(dvp.value)
print int(svp)


I think the same thing could also be accomplished entirely in c++, not sure if 
it would be worth it.

Matt


More information about the PyQt mailing list