Search code examples
pythonqtpyqtpyside

How to csat WinID handle to capsule in pyside6


In pyqt, one can access wid as sip.voidptr, using widget.winId(), and then turn it to capsule object using wid.ascapsule()

In pyside6, when using widget.winId(), I can only get an int number.

How can I get winId() as ptr like pyqt when using pyside6?

I need ptr as capsule because the api I use give the following error:

return WNT_Window(ctypes.c_void_p(wid))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. OCP.WNT.WNT_Window(theTitle: str, theClass: OCP.WNT.WNT_WClass, theStyle: int, thePxLeft: int, thePxTop: int, thePxWidth: int, thePxHeight: int, theBackColor: OCP.Quantity.Quantity_NameOfColor = <Quantity_NameOfColor.Quantity_NOC_MATRAGRAY: 2>, theParent: capsule = None, theMenu: capsule = None, theClientStruct: capsule = None)
    2. OCP.WNT.WNT_Window(theHandle: capsule, theBackColor: OCP.Quantity.Quantity_NameOfColor = <Quantity_NameOfColor.Quantity_NOC_MATRAGRAY: 2>)

Solution

  • Finally get it works, with great thanks to @ekhumoro.

    My solution:

    1.convert winId to void_pointer

    wid = widget.winId
    ptr = ctypes.cast(wid, ctypes.POINTER(ctypes.c_void_p)) 
    
    1. create PyCapsule
    PyCapsule_Destructor = ctypes.CFUNCTYPE(None, ctypes.py_object)
    PyCapsule_New = ctypes.pythonapi.PyCapsule_New
    PyCapsule_New.restype = ctypes.py_object
    PyCapsule_New.argtypes = (ctypes.c_void_p, ctypes.c_char_p, PyCapsule_Destructor)
    capsule = PyCapsule_New(ptr, None, PyCapsule_Destructor(0))