SourceForge.net Logo
PythonQt
Classes | Macros | Typedefs | Functions
PythonQtConversion.h File Reference
#include "PythonQt.h"
#include "PythonQtMisc.h"
#include "PythonQtClassInfo.h"
#include "PythonQtMethodInfo.h"
#include <QList>
#include <vector>

Go to the source code of this file.

Classes

class  PythonQtConv
 a static class that offers methods for type conversion More...
 

Macros

#define PythonQtRegisterListTemplateConverter(type, innertype)
 
#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype)
 
#define PythonQtRegisterQPairConverter(type1, type2)
 
#define PythonQtRegisterIntegerMapConverter(type, innertype)
 
#define PythonQtRegisterListTemplateQPairConverter(listtype, type1, type2)
 
#define PythonQtRegisterToolClassesTemplateConverter(innertype)
 
#define PythonQtRegisterToolClassesTemplateConverterForKnownClass(innertype)
 

Typedefs

typedef PyObjectPythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)
 
typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)
 

Functions

template<class ListType , class T >
PyObjectPythonQtConvertListOfValueTypeToPythonList (const void *inList, int metaTypeId)
 
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfValueType (PyObject *obj, void *outList, int metaTypeId, bool)
 
template<class ListType , class T >
PyObjectPythonQtConvertListOfKnownClassToPythonList (const void *inList, int metaTypeId)
 
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfKnownClass (PyObject *obj, void *outList, int metaTypeId, bool)
 
template<class T1 , class T2 >
PyObjectPythonQtConvertPairToPython (const void *inPair, int metaTypeId)
 
template<class T1 , class T2 >
bool PythonQtConvertPythonToPair (PyObject *obj, void *outPair, int metaTypeId, bool)
 
template<class ListType , class T1 , class T2 >
PyObjectPythonQtConvertListOfPairToPythonList (const void *inList, int metaTypeId)
 
template<class ListType , class T1 , class T2 >
bool PythonQtConvertPythonListToListOfPair (PyObject *obj, void *outList, int metaTypeId, bool)
 
template<class MapType , class T >
PyObjectPythonQtConvertIntegerMapToPython (const void *inMap, int metaTypeId)
 
template<class MapType , class T >
bool PythonQtConvertPythonToIntegerMap (PyObject *val, void *outMap, int metaTypeId, bool)
 

Detailed Description

Author
Florian Link
Last changed by
Author
florian
Date
2006-05

Definition in file PythonQtConversion.h.

Macro Definition Documentation

#define PythonQtRegisterIntegerMapConverter (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<int, innertype > >(#type"<int, "#innertype">"); \
}
bool PythonQtConvertPythonToIntegerMap(PyObject *val, void *outMap, int metaTypeId, bool)
PyObject * PythonQtConvertIntegerMapToPython(const void *inMap, int metaTypeId)
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype

Definition at line 74 of file PythonQtConversion.h.

#define PythonQtRegisterListTemplateConverter (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
}
bool PythonQtConvertPythonListToListOfValueType(PyObject *obj, void *outList, int metaTypeId, bool)
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
PyObject * PythonQtConvertListOfValueTypeToPythonList(const void *inList, int metaTypeId)
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype

Definition at line 56 of file PythonQtConversion.h.

#define PythonQtRegisterListTemplateConverterForKnownClass (   type,
  innertype 
)
Value:
{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
}
PyObject * PythonQtConvertListOfKnownClassToPythonList(const void *inList, int metaTypeId)
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype
bool PythonQtConvertPythonListToListOfKnownClass(PyObject *obj, void *outList, int metaTypeId, bool)

Definition at line 62 of file PythonQtConversion.h.

#define PythonQtRegisterListTemplateQPairConverter (   listtype,
  type1,
  type2 
)
Value:
{ \
qRegisterMetaType<QPair<type1, type2> >("QPair<"#type1","#type2">"); \
int typeId = qRegisterMetaType<listtype<QPair<type1, type2> > >(#listtype"<QPair<"#type1","#type2">>"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfPair<listtype<QPair<type1, type2> >, type1, type2>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfPairToPythonList<listtype<QPair<type1, type2> >, type1, type2>); \
}
PyObject * PythonQtConvertListOfPairToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonListToListOfPair(PyObject *obj, void *outList, int metaTypeId, bool)
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype

Definition at line 80 of file PythonQtConversion.h.

#define PythonQtRegisterQPairConverter (   type1,
  type2 
)
Value:
{ int typeId = qRegisterMetaType<QPair<type1, type2> >("QPair<"#type1","#type2">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToPair<type1, type2>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertPairToPython<type1, type2>); \
}
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype

Definition at line 68 of file PythonQtConversion.h.

#define PythonQtRegisterToolClassesTemplateConverter (   innertype)
Value:
#define PythonQtRegisterListTemplateConverter(type, innertype)

Definition at line 88 of file PythonQtConversion.h.

#define PythonQtRegisterToolClassesTemplateConverterForKnownClass (   innertype)
Value:
#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype)

Definition at line 93 of file PythonQtConversion.h.

Typedef Documentation

typedef PyObject* PythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)

Definition at line 53 of file PythonQtConversion.h.

typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)

Definition at line 54 of file PythonQtConversion.h.

Function Documentation

template<class MapType , class T >
PyObject* PythonQtConvertIntegerMapToPython ( const void *  inMap,
int  metaTypeId 
)

Definition at line 447 of file PythonQtConversion.h.

References PythonQtConv::convertQtValueToPythonInternal(), PythonQtMethodInfo::getInnerTemplateTypeName(), and PythonQtUtils::typeName().

448 {
449  MapType* map = (MapType*)inMap;
450  static int innerType = -1;
451  if (innerType == -1) {
452  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
453  QList<QByteArray> names = innerTypes.split(',');
454  innerType = QMetaType::type(names.at(1).trimmed());
455  }
456  if (innerType == QVariant::Invalid) {
457  std::cerr << "PythonQtConvertIntegerMapToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
458  }
459 
460  PyObject* result = PyDict_New();
461  typename MapType::const_iterator t = map->constBegin();
462  PyObject* key;
463  PyObject* val;
464  for (; t != map->constEnd(); t++) {
465  key = PyInt_FromLong(t.key());
466  val = PythonQtConv::convertQtValueToPythonInternal(innerType, &t.value());
467  PyDict_SetItem(result, key, val);
468  Py_DECREF(key);
469  Py_DECREF(val);
470  }
471  return result;
472 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject ...
struct _object PyObject
static PyObject * convertQtValueToPythonInternal(int type, const void *data)
converts the Qt parameter given in data, interpreting it as a type registered qvariant/meta type...
template<class ListType , class T >
PyObject* PythonQtConvertListOfKnownClassToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 266 of file PythonQtConversion.h.

References PythonQtInstanceWrapperStruct::_ownedByPythonQt, PythonQtClassInfo::className(), PythonQtPrivate::getClassInfo(), PythonQtMethodInfo::getInnerListTypeName(), PythonQt::priv(), PythonQtUtils::typeName(), and PythonQtPrivate::wrapPtr().

267 {
268  ListType* list = (ListType*)inList;
270  if (innerType == NULL) {
271  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
272  }
273  PyObject* result = PyTuple_New(list->size());
274  int i = 0;
275  Q_FOREACH(const T& value, *list) {
276  T* newObject = new T(value);
278  wrap->_ownedByPythonQt = true;
279  PyTuple_SET_ITEM(result, i, (PyObject*)wrap);
280  i++;
281  }
282  return result;
283 }
PyObject * wrapPtr(void *ptr, const QByteArray &name, bool passOwnership=false)
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...
const QByteArray & className() const
get the classname (either of the QObject or of the wrapped CPP object)
a class that stores all required information about a Qt object (and an optional associated C++ class ...
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static PythonQtPrivate * priv()
get access to internal data (should not be used on the public API, but is used by some C functions) ...
Definition: PythonQt.h:529
bool _ownedByPythonQt
flag that stores if the object is owned by pythonQt
struct _object PyObject
static QByteArray getInnerListTypeName(const QByteArray &typeName)
returns the inner type name of a simple template or the typename without appended "List"...
PythonQtClassInfo * getClassInfo(const QMetaObject *meta)
get the class info for a meta object (if available)
template<class ListType , class T1 , class T2 >
PyObject* PythonQtConvertListOfPairToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 395 of file PythonQtConversion.h.

References PythonQtMethodInfo::getInnerTemplateMetaType(), and PythonQtUtils::typeName().

396 {
397  ListType* list = (ListType*)inList;
398  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
399  if (innerType == QVariant::Invalid) {
400  std::cerr << "PythonQtConvertListOfPairToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
401  }
402  PyObject* result = PyTuple_New(list->size());
403  int i = 0;
404  typedef const QPair<T1, T2> Pair;
405  Q_FOREACH(Pair& value, *list) {
406  PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
407  PyTuple_SET_ITEM(result, i, object);
408  i++;
409  }
410  return result;
411 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject
struct _object PyObject
template<class ListType , class T >
PyObject* PythonQtConvertListOfValueTypeToPythonList ( const void *  inList,
int  metaTypeId 
)

Definition at line 216 of file PythonQtConversion.h.

References PythonQtConv::convertQtValueToPythonInternal(), PythonQtMethodInfo::getInnerTemplateMetaType(), and PythonQtUtils::typeName().

217 {
218  ListType* list = (ListType*)inList;
219  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
220  if (innerType == QVariant::Invalid) {
221  std::cerr << "PythonQtConvertListOfValueTypeToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
222  }
223  PyObject* result = PyTuple_New(list->size());
224  int i = 0;
225  Q_FOREACH (const T& value, *list) {
226  PyTuple_SET_ITEM(result, i, PythonQtConv::convertQtValueToPythonInternal(innerType, &value));
227  i++;
228  }
229  return result;
230 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject
struct _object PyObject
static PyObject * convertQtValueToPythonInternal(int type, const void *data)
converts the Qt parameter given in data, interpreting it as a type registered qvariant/meta type...
template<class T1 , class T2 >
PyObject* PythonQtConvertPairToPython ( const void *  inPair,
int  metaTypeId 
)

Definition at line 326 of file PythonQtConversion.h.

References PythonQtConv::convertQtValueToPythonInternal(), PythonQtMethodInfo::getInnerTemplateTypeName(), and PythonQtUtils::typeName().

327 {
328  QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
329  static int innerType1 = -1;
330  static int innerType2 = -1;
331  if (innerType1==-1) {
332  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
333  QList<QByteArray> names = innerTypes.split(',');
334  innerType1 = QMetaType::type(names.at(0).trimmed());
335  innerType2 = QMetaType::type(names.at(1).trimmed());
336  }
337  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
338  std::cerr << "PythonQtConvertPairToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
339  }
340  PyObject* result = PyTuple_New(2);
341  PyTuple_SET_ITEM(result, 0, PythonQtConv::convertQtValueToPythonInternal(innerType1, &pair->first));
342  PyTuple_SET_ITEM(result, 1, PythonQtConv::convertQtValueToPythonInternal(innerType2, &pair->second));
343  return result;
344 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject ...
struct _object PyObject
static PyObject * convertQtValueToPythonInternal(int type, const void *data)
converts the Qt parameter given in data, interpreting it as a type registered qvariant/meta type...
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfKnownClass ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 286 of file PythonQtConversion.h.

References PythonQtConv::castWrapperTo(), PythonQtClassInfo::className(), PythonQtPrivate::getClassInfo(), PythonQtMethodInfo::getInnerListTypeName(), PythonQt::priv(), PythonQtInstanceWrapper_Type, and PythonQtUtils::typeName().

287 {
288  ListType* list = (ListType*)outList;
290  if (innerType == NULL) {
291  std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->className().constData() << std::endl;
292  }
293  bool result = false;
294  if (PySequence_Check(obj)) {
295  int count = PySequence_Size(obj);
296  if (count >= 0) {
297  result = true;
298  PyObject* value;
299  for (int i = 0; i < count; i++) {
300  value = PySequence_GetItem(obj, i);
301  if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
303  bool ok;
304  T* object = (T*)PythonQtConv::castWrapperTo(wrap, innerType->className(), ok);
305  Py_XDECREF(value);
306  if (ok) {
307  list->push_back(*object);
308  } else {
309  result = false;
310  break;
311  }
312  } else {
313  Py_XDECREF(value);
314  result = false;
315  break;
316  }
317  }
318  }
319  }
320  return result;
321 }
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...
const QByteArray & className() const
get the classname (either of the QObject or of the wrapped CPP object)
a class that stores all required information about a Qt object (and an optional associated C++ class ...
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static PythonQtPrivate * priv()
get access to internal data (should not be used on the public API, but is used by some C functions) ...
Definition: PythonQt.h:529
static void * castWrapperTo(PythonQtInstanceWrapper *wrapper, const QByteArray &className, bool &ok)
cast wrapper to given className if possible
struct _object PyObject
static QByteArray getInnerListTypeName(const QByteArray &typeName)
returns the inner type name of a simple template or the typename without appended "List"...
PythonQtClassInfo * getClassInfo(const QMetaObject *meta)
get the class info for a meta object (if available)
PYTHONQT_EXPORT PyTypeObject PythonQtInstanceWrapper_Type
template<class ListType , class T1 , class T2 >
bool PythonQtConvertPythonListToListOfPair ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 414 of file PythonQtConversion.h.

References PythonQtMethodInfo::getInnerTemplateMetaType(), and PythonQtUtils::typeName().

415 {
416  ListType* list = (ListType*)outList;
417  static int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
418  if (innerType == QVariant::Invalid) {
419  std::cerr << "PythonQtConvertPythonListToListOfPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
420  }
421  bool result = false;
422  if (PySequence_Check(obj)) {
423  int count = PySequence_Size(obj);
424  if (count >= 0) {
425  result = true;
426  PyObject* value;
427  for (int i = 0; i < count; i++) {
428  QPair<T1, T2> pair;
429  value = PySequence_GetItem(obj, i);
430  if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType, false)) {
431  Py_XDECREF(value);
432  list->push_back(pair);
433  } else {
434  Py_XDECREF(value);
435  result = false;
436  break;
437  }
438  }
439  }
440  }
441  return result;
442 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject
struct _object PyObject
template<class ListType , class T >
bool PythonQtConvertPythonListToListOfValueType ( PyObject obj,
void *  outList,
int  metaTypeId,
bool   
)

Definition at line 233 of file PythonQtConversion.h.

References PythonQtMethodInfo::getInnerTemplateMetaType(), PythonQtConv::PyObjToQVariant(), and PythonQtUtils::typeName().

234 {
235  ListType* list = (ListType*)outList;
236  static const int innerType = PythonQtMethodInfo::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId)));
237  if (innerType == QVariant::Invalid) {
238  std::cerr << "PythonQtConvertPythonListToListOfValueType: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
239  }
240  bool result = false;
241  if (PySequence_Check(obj)) {
242  int count = PySequence_Size(obj);
243  if (count >= 0) {
244  result = true;
245  PyObject* value;
246  for (int i = 0;i<count;i++) {
247  value = PySequence_GetItem(obj,i);
248  // this is quite some overhead, but it avoids having another large switch...
249  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
250  Py_XDECREF(value);
251  if (v.isValid()) {
252  list->push_back(qvariant_cast<T>(v));
253  } else {
254  result = false;
255  break;
256  }
257  }
258  }
259  }
260  return result;
261 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject
struct _object PyObject
static QVariant PyObjToQVariant(PyObject *val, int type=-1)
template<class MapType , class T >
bool PythonQtConvertPythonToIntegerMap ( PyObject val,
void *  outMap,
int  metaTypeId,
bool   
)

Definition at line 475 of file PythonQtConversion.h.

References PythonQtMethodInfo::getInnerTemplateTypeName(), PythonQtConv::PyObjGetInt(), PythonQtConv::PyObjToQVariant(), and PythonQtUtils::typeName().

476 {
477  MapType* map = (MapType*)outMap;
478  static int innerType = -1;
479  if (innerType == -1) {
480  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
481  QList<QByteArray> names = innerTypes.split(',');
482  innerType = QMetaType::type(names.at(1).trimmed());
483  }
484  if (innerType == QVariant::Invalid) {
485  std::cerr << "PythonQtConvertPythonToIntegerMap: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
486  }
487  bool result = false;
488  if (PyMapping_Check(val)) {
489  result = true;
490  PyObject* items = PyMapping_Items(val);
491  if (items) {
492  int count = PyList_Size(items);
493  PyObject* value;
494  PyObject* key;
495  PyObject* tuple;
496  for (int i = 0; i < count; i++) {
497  tuple = PyList_GetItem(items, i);
498  key = PyTuple_GetItem(tuple, 0);
499  value = PyTuple_GetItem(tuple, 1);
500 
501  bool ok;
502  int intKey = PythonQtConv::PyObjGetInt(key, false, ok);
503  // this is quite some overhead, but it avoids having another large switch...
504  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
505  if (v.isValid() && ok) {
506  map->insert(intKey, qvariant_cast<T>(v));
507  } else {
508  result = false;
509  break;
510  }
511  }
512  Py_DECREF(items);
513  }
514  }
515  return result;
516 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject ...
struct _object PyObject
static int PyObjGetInt(PyObject *val, bool strict, bool &ok)
get int from py object
static QVariant PyObjToQVariant(PyObject *val, int type=-1)
template<class T1 , class T2 >
bool PythonQtConvertPythonToPair ( PyObject obj,
void *  outPair,
int  metaTypeId,
bool   
)

Definition at line 347 of file PythonQtConversion.h.

References PythonQtMethodInfo::getInnerTemplateTypeName(), PythonQtConv::PyObjToQVariant(), and PythonQtUtils::typeName().

348 {
349  QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
350  static int innerType1 = -1;
351  static int innerType2 = -1;
352  if (innerType1 == -1) {
353  QByteArray innerTypes = PythonQtMethodInfo::getInnerTemplateTypeName(QByteArray(QMetaType::typeName(metaTypeId)));
354  QList<QByteArray> names = innerTypes.split(',');
355  innerType1 = QMetaType::type(names.at(0).trimmed());
356  innerType2 = QMetaType::type(names.at(1).trimmed());
357  }
358  if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
359  std::cerr << "PythonQtConvertPythonToPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
360  }
361  bool result = false;
362  if (PySequence_Check(obj)) {
363  int count = PySequence_Size(obj);
364  if (count == 2) {
365  result = true;
366  PyObject* value;
367 
368  value = PySequence_GetItem(obj, 0);
369  // this is quite some overhead, but it avoids having another large switch...
370  QVariant v = PythonQtConv::PyObjToQVariant(value, innerType1);
371  Py_XDECREF(value);
372  if (v.isValid()) {
373  pair->first = qvariant_cast<T1>(v);
374  } else {
375  return false;
376  }
377 
378  value = PySequence_GetItem(obj, 1);
379  // this is quite some overhead, but it avoids having another large switch...
380  v = PythonQtConv::PyObjToQVariant(value, innerType2);
381  Py_XDECREF(value);
382  if (v.isValid()) {
383  pair->second = qvariant_cast<T2>(v);
384  } else {
385  return false;
386  }
387  }
388  }
389  return result;
390 }
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject ...
struct _object PyObject
static QVariant PyObjToQVariant(PyObject *val, int type=-1)