Skip to content

Commit d539739

Browse files
committed
Add __getattr__ and __setattr__ to QgsFeature
1 parent d004791 commit d539739

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

python/core/qgsfeature.sip

+54
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,60 @@ class QgsFeature
3030

3131
public:
3232

33+
SIP_PYOBJECT __getattr__(const QString& name);
34+
%MethodCode
35+
int fieldIdx = sipCpp->fieldNameIndex(*a0);
36+
QString altname = QString(*a0).replace("_"," ");
37+
int altfieldIdx = sipCpp->fieldNameIndex(altname);
38+
39+
if (fieldIdx >= 0)
40+
{
41+
QVariant* v = new QVariant( sipCpp->attribute(fieldIdx) );
42+
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
43+
}
44+
else if( altfieldIdx >= 0 )
45+
{
46+
QVariant* v = new QVariant( sipCpp->attribute(altfieldIdx) );
47+
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
48+
}
49+
else
50+
{
51+
PyObject* key = PyString_FromString(a0->toStdString().c_str());
52+
sipRes = PyObject_GenericGetAttr(sipSelf, key );
53+
}
54+
%End
55+
56+
void __setattr__(const QString& key, QVariant value);
57+
%MethodCode
58+
int fieldIdx = sipCpp->fieldNameIndex(*a0);
59+
QString altname = QString(*a0).replace("_"," ");
60+
int altfieldIdx = sipCpp->fieldNameIndex(altname);
61+
62+
if (fieldIdx >= 0)
63+
{
64+
sipCpp->setAttribute(fieldIdx, *a1);
65+
}
66+
else if( altfieldIdx >= 0 )
67+
{
68+
sipCpp->setAttribute(altfieldIdx, *a1);
69+
}
70+
else
71+
{
72+
PyObject* key = PyString_FromString(a0->toStdString().c_str());
73+
PyObject* value = sipConvertFromType( a1, sipType_QVariant, sipSelf);
74+
PyObject_GenericSetAttr(sipSelf, key, value);
75+
}
76+
%End
77+
78+
void __delattr__(const QString& key);
79+
%MethodCode
80+
int fieldIdx = sipCpp->fieldNameIndex(*a0);
81+
if (fieldIdx == -1)
82+
PyErr_SetString(PyExc_KeyError, a0->toAscii());
83+
else
84+
sipCpp->deleteAttribute(fieldIdx);
85+
%End
86+
3387
SIP_PYOBJECT __getitem__(int key);
3488
%MethodCode
3589
const QgsAttributes& attrs = sipCpp->attributes();

0 commit comments

Comments
 (0)