2012
11.14
11.14
sip.wrapinstance returns the best possible matching class while shiboken.wrapInstance does not. This is a pain to deal with, because you have to know the class of the object before you get it…
Here’s my shiboken wrapInstance function that actually gives back the correct class instead (This function will allow you to wrapinstance for either PyQt or PySide and get the same result back).
import shiboken from PySide import QtGui, QtCore def wrapinstance(ptr, base=None): """ Utility to convert a pointer to a Qt class instance (PySide/PyQt compatible) :param ptr: Pointer to QObject in memory :type ptr: long or Swig instance :param base: (Optional) Base class to wrap with (Defaults to QObject, which should handle anything) :type base: QtGui.QWidget :return: QWidget or subclass instance :rtype: QtGui.QWidget """ if ptr is None: return None ptr = long(ptr) #Ensure type if globals().has_key('shiboken'): if base is None: qObj = shiboken.wrapInstance(long(ptr), QtCore.QObject) metaObj = qObj.metaObject() cls = metaObj.className() superCls = metaObj.superClass().className() if hasattr(QtGui, cls): base = getattr(QtGui, cls) elif hasattr(QtGui, superCls): base = getattr(QtGui, superCls) else: base = QtGui.QWidget return shiboken.wrapInstance(long(ptr), base) elif globals().has_key('sip'): base = QtCore.QObject return sip.wrapinstance(long(ptr), base) else: return None