From 1db88b846b4ae41e2d452aae05fc51ad8157efaf 2009-05-19 08:44:05 From: florianlink Date: 2009-05-19 08:44:05 Subject: [PATCH] added support for return by value of classes that are NOT registered with QMetaType but which have a default constructor decorator git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@82 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/src/PythonQtSlot.cpp b/src/PythonQtSlot.cpp index 19e1cb7..16f19fe 100644 --- a/src/PythonQtSlot.cpp +++ b/src/PythonQtSlot.cpp @@ -134,6 +134,20 @@ bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObj if (!directReturnValuePointer) { // create empty default value for the return value argList[0] = PythonQtConv::CreateQtReturnValue(returnValueParam); + if (argList[0]==NULL) { + // return value could not be created, maybe we have a registered class with a default constructor, so that we can construct the pythonqt wrapper object and + // pass its internal pointer + PythonQtClassInfo* info = PythonQt::priv()->getClassInfo(returnValueParam.name); + if (info && info->pythonQtClassWrapper()) { + PyObject* emptyTuple = PyTuple_New(0); + // 1) default construct an empty object as python object (owned by PythonQt), by calling the meta class with empty arguments + result = PyObject_Call((PyObject*)info->pythonQtClassWrapper(), emptyTuple, NULL); + if (result) { + argList[0] = ((PythonQtInstanceWrapper*)result)->_wrappedPtr; + } + Py_DECREF(emptyTuple); + } + } } else { // we can use our pointer directly! argList[0] = directReturnValuePointer; @@ -150,13 +164,16 @@ bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObj result = NULL; } else { if (!returnValueIsEnum) { - result = PythonQtConv::ConvertQtValueToPython(returnValueParam, argList[0]); + // the resulting object maybe present already, because we created it above at 1)... + if (!result) { + result = PythonQtConv::ConvertQtValueToPython(returnValueParam, argList[0]); + } } else { result = PyInt_FromLong(*((unsigned int*)argList[0])); } } } else { - QString e = QString("Called ") + info->fullSignature() + ", return type '" + returnValueParam.name + "' is ignored because it is unknown to PythonQt. Probaby you should register it using qRegisterMetaType()."; + QString e = QString("Called ") + info->fullSignature() + ", return type '" + returnValueParam.name + "' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class."; PyErr_SetString(PyExc_ValueError, e.toLatin1().data()); result = NULL; }