From ab319fdfc38dd2e4433c1eed176a887066c9a7a7 2010-03-06 21:41:46 From: florianlink Date: 2010-03-06 21:41:46 Subject: [PATCH] added initial support for public member variable access via getters/setters git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@150 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/src/PythonQtInstanceWrapper.cpp b/src/PythonQtInstanceWrapper.cpp index f4b97cf..a2e32ed 100644 --- a/src/PythonQtInstanceWrapper.cpp +++ b/src/PythonQtInstanceWrapper.cpp @@ -300,11 +300,19 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name) } break; case PythonQtMemberInfo::NotFound: - // handle dynamic properties - if (wrapper->_obj) { - QVariant v = wrapper->_obj->property(attributeName); - if (v.isValid()) { - return PythonQtConv::QVariantToPyObject(v); + { + // check for a getter_ + PythonQtMemberInfo member = wrapper->classInfo()->member(QByteArray("getter_") + attributeName); + if (member._type == PythonQtMemberInfo::Slot) { + return PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, member._slot, NULL, NULL, wrapper->_wrappedPtr); + } + + // handle dynamic properties + if (wrapper->_obj) { + QVariant v = wrapper->_obj->property(attributeName); + if (v.isValid()) { + return PythonQtConv::QVariantToPyObject(v); + } } } break; @@ -339,7 +347,7 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name) static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value) { QString error; - char *attributeName; + const char *attributeName; PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj; if ((attributeName = PyString_AsString(name)) == NULL) @@ -385,6 +393,19 @@ static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObjec } else if (member._type == PythonQtMemberInfo::EnumWrapper) { error = QString("Enum '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; } else if (member._type == PythonQtMemberInfo::NotFound) { + // check for a setter_ + PythonQtMemberInfo setter = wrapper->classInfo()->member(QByteArray("setter_") + attributeName); + if (setter._type == PythonQtMemberInfo::Slot) { + // call the setter and ignore the result value + void* result; + PyObject* args = PyTuple_New(1); + Py_INCREF(value); + PyTuple_SET_ITEM(args, 0, value); + PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, setter._slot, args, NULL, wrapper->_wrappedPtr, &result); + Py_DECREF(args); + return 0; + } + // handle dynamic properties if (wrapper->_obj) { QVariant prop = wrapper->_obj->property(attributeName); diff --git a/src/PythonQtSlot.cpp b/src/PythonQtSlot.cpp index ee4ff14..556ebbf 100644 --- a/src/PythonQtSlot.cpp +++ b/src/PythonQtSlot.cpp @@ -233,7 +233,7 @@ PyObject *PythonQtSlotFunction_Call(PyObject *func, PyObject *args, PyObject *kw PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* objectToCall, PythonQtSlotInfo* info, PyObject *args, PyObject * /*kw*/, void* firstArg, void** directReturnValuePointer) { - int argc = PyTuple_Size(args); + int argc = args?PyTuple_Size(args):0; #ifdef PYTHONQT_DEBUG std::cout << "called " << info->metaMethod()->typeName() << " " << info->metaMethod()->signature() << std::endl;