##// END OF EJS Templates
added support for dynamic properties...
florianlink -
r85:90c5da76b56c
parent child
Show More
@@ -118,6 +118,8 int PythonQtClassInfo::findCharOffset(const char* sigStart, char someChar)
118 118
119 119 bool PythonQtClassInfo::lookForPropertyAndCache(const char* memberName)
120 120 {
121 if (!_meta) return false;
122
121 123 bool found = false;
122 124 bool nameMapped = false;
123 125 const char* attributeName = memberName;
@@ -241,6 +241,17 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
241 241 std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data();
242 242 }
243 243 }
244
245 QList<QByteArray> dynamicProps = wrapper->_obj->dynamicPropertyNames();
246 foreach (QByteArray name, dynamicProps) {
247 PyObject* o = PyObject_GetAttrString(obj, name.data());
248 if (o) {
249 PyDict_SetItemString(dict, name.data(), o);
250 Py_DECREF(o);
251 } else {
252 std::cerr << "PythonQtInstanceWrapper: dynamic property could not be read " << name.data();
253 }
254 }
244 255 }
245 256 // Note: we do not put children into the dict, is would look confusing?!
246 257 return dict;
@@ -255,8 +266,6 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
255 266
256 267 // mlabDebugConst("Python","get " << attributeName);
257 268
258 // TODO: dynamic properties are missing
259
260 269 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
261 270 switch (member._type) {
262 271 case PythonQtMemberInfo::Property:
@@ -290,6 +299,15 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
290 299 return enumWrapper;
291 300 }
292 301 break;
302 case PythonQtMemberInfo::NotFound:
303 // handle dynamic properties
304 if (wrapper->_obj) {
305 QVariant v = wrapper->_obj->property(attributeName);
306 if (v.isValid()) {
307 return PythonQtConv::QVariantToPyObject(v);
308 }
309 }
310 break;
293 311 default:
294 312 // is an invalid type, go on
295 313 break;
@@ -367,6 +385,23 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObjec
367 385 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
368 386 error = QString("Enum '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
369 387 } else if (member._type == PythonQtMemberInfo::NotFound) {
388 // handle dynamic properties
389 if (wrapper->_obj) {
390 QVariant prop = wrapper->_obj->property(attributeName);
391 if (prop.isValid()) {
392 QVariant v = PythonQtConv::PyObjToQVariant(value);
393 if (v.isValid()) {
394 wrapper->_obj->setProperty(attributeName, v);
395 return 0;
396 } else {
397 error = QString("Dynamic property '") + attributeName + "' does not accept an object of type "
398 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
399 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
400 return -1;
401 }
402 }
403 }
404
370 405 // if we are a derived python class, we allow setting attributes.
371 406 // if we are a direct CPP wrapper, we do NOT allow it, since
372 407 // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore
@@ -149,6 +149,16 const QObjectList* PythonQtStdDecorators::children(QObject* o)
149 149 return &o->children();
150 150 }
151 151
152 bool PythonQtStdDecorators::setProperty(QObject* o, const char* name, const QVariant& value)
153 {
154 return o->setProperty(name, value);
155 }
156
157 QVariant PythonQtStdDecorators::property(QObject* o, const char* name)
158 {
159 return o->property(name);
160 }
161
152 162 QString PythonQtStdDecorators::tr(QObject* obj, const QByteArray& text, const QByteArray& ambig, int n)
153 163 {
154 164 return QCoreApplication::translate(obj->metaObject()->className(), text.constData(), ambig.constData(), QCoreApplication::CodecForTr, n);
@@ -75,6 +75,9 public slots:
75 75 QList<QObject*> findChildren(QObject* parent, PyObject* type, const QString& name= QString());
76 76 QList<QObject*> findChildren(QObject* parent, PyObject* type, const QRegExp& regExp);
77 77
78 bool setProperty(QObject* o, const char* name, const QVariant& value);
79 QVariant property(QObject* o, const char* name);
80
78 81 double static_Qt_qAbs(double a) { return qAbs(a); }
79 82 double static_Qt_qBound(double a,double b,double c) { return qBound(a,b,c); }
80 83 void static_Qt_qDebug(const QByteArray& msg) { qDebug(msg.constData()); }
General Comments 0
You need to be logged in to leave comments. Login now