PythonQt
PythonQtConversion.h File Reference
#include "PythonQtPythonInclude.h"
#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 PyObject * PythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)
typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)
typedef QVariant PythonQtConvertPythonSequenceToQVariantListCB(PyObject *inObject)

Functions

template<class ListType, class T>
PyObject * PythonQtConvertListOfValueTypeToPythonList (const void *inList, int metaTypeId)
template<class ListType, class T>
bool PythonQtConvertPythonListToListOfValueType (PyObject *obj, void *outList, int metaTypeId, bool)
template<class ListType, class T>
PyObject * PythonQtConvertListOfKnownClassToPythonList (const void *inList, int metaTypeId)
template<class ListType, class T>
bool PythonQtConvertPythonListToListOfKnownClass (PyObject *obj, void *outList, int metaTypeId, bool)
template<class T1, class T2>
PyObject * PythonQtConvertPairToPython (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>
PyObject * PythonQtConvertListOfPairToPythonList (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>
PyObject * PythonQtConvertIntegerMapToPython (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

◆ PythonQtRegisterIntegerMapConverter

#define PythonQtRegisterIntegerMapConverter ( type,
innertype )
Value:
{ \
int typeId = qRegisterMetaType<type<int, innertype>>(#type "<int, " #innertype ">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
PythonQtConvertPythonToIntegerMap<type<int, innertype>, innertype>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
PythonQtConvertIntegerMapToPython<type<int, innertype>, innertype>); \
}
PyObject * PythonQtConvertIntegerMapToPython(const void *inMap, int metaTypeId)
bool PythonQtConvertPythonToIntegerMap(PyObject *val, void *outMap, int metaTypeId, bool)

Definition at line 83 of file PythonQtConversion.h.

83#define PythonQtRegisterIntegerMapConverter(type, innertype) \
84 { \
85 int typeId = qRegisterMetaType<type<int, innertype>>(#type "<int, " #innertype ">"); \
86 PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
87 PythonQtConvertPythonToIntegerMap<type<int, innertype>, innertype>); \
88 PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
89 PythonQtConvertIntegerMapToPython<type<int, innertype>, innertype>); \
90 }

◆ PythonQtRegisterListTemplateConverter

#define PythonQtRegisterListTemplateConverter ( type,
innertype )
Value:
{ \
int typeId = qRegisterMetaType<type<innertype>>(#type "<" #innertype ">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
PythonQtConvertPythonListToListOfValueType<type<innertype>, innertype>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
PythonQtConvertListOfValueTypeToPythonList<type<innertype>, innertype>); \
}
PyObject * PythonQtConvertListOfValueTypeToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonListToListOfValueType(PyObject *obj, void *outList, int metaTypeId, bool)

Definition at line 58 of file PythonQtConversion.h.

58#define PythonQtRegisterListTemplateConverter(type, innertype) \
59 { \
60 int typeId = qRegisterMetaType<type<innertype>>(#type "<" #innertype ">"); \
61 PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
62 PythonQtConvertPythonListToListOfValueType<type<innertype>, innertype>); \
63 PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
64 PythonQtConvertListOfValueTypeToPythonList<type<innertype>, innertype>); \
65 }

◆ PythonQtRegisterListTemplateConverterForKnownClass

#define PythonQtRegisterListTemplateConverterForKnownClass ( type,
innertype )
Value:
{ \
int typeId = qRegisterMetaType<type<innertype>>(#type "<" #innertype ">"); \
PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
PythonQtConvertPythonListToListOfKnownClass<type<innertype>, innertype>); \
PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
PythonQtConvertListOfKnownClassToPythonList<type<innertype>, innertype>); \
}
bool PythonQtConvertPythonListToListOfKnownClass(PyObject *obj, void *outList, int metaTypeId, bool)
PyObject * PythonQtConvertListOfKnownClassToPythonList(const void *inList, int metaTypeId)

Definition at line 67 of file PythonQtConversion.h.

67#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype) \
68 { \
69 int typeId = qRegisterMetaType<type<innertype>>(#type "<" #innertype ">"); \
70 PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
71 PythonQtConvertPythonListToListOfKnownClass<type<innertype>, innertype>); \
72 PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
73 PythonQtConvertListOfKnownClassToPythonList<type<innertype>, innertype>); \
74 }

◆ PythonQtRegisterListTemplateQPairConverter

#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>); \
}
bool PythonQtConvertPythonListToListOfPair(PyObject *obj, void *outList, int metaTypeId, bool)
PyObject * PythonQtConvertListOfPairToPythonList(const void *inList, int metaTypeId)

Definition at line 92 of file PythonQtConversion.h.

92#define PythonQtRegisterListTemplateQPairConverter(listtype, type1, type2) \
93 { \
94 qRegisterMetaType<QPair<type1, type2>>("QPair<" #type1 "," #type2 ">"); \
95 int typeId = qRegisterMetaType<listtype<QPair<type1, type2>>>(#listtype "<QPair<" #type1 "," #type2 ">>"); \
96 PythonQtConv::registerPythonToMetaTypeConverter(typeId, \
97 PythonQtConvertPythonListToListOfPair<listtype<QPair<type1, type2>>, type1, type2>); \
98 PythonQtConv::registerMetaTypeToPythonConverter(typeId, \
99 PythonQtConvertListOfPairToPythonList<listtype<QPair<type1, type2>>, type1, type2>); \
100 }

◆ PythonQtRegisterQPairConverter

#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>); \
}
PyObject * PythonQtConvertPairToPython(const void *inPair, int metaTypeId)
bool PythonQtConvertPythonToPair(PyObject *obj, void *outPair, int metaTypeId, bool)

Definition at line 76 of file PythonQtConversion.h.

76#define PythonQtRegisterQPairConverter(type1, type2) \
77 { \
78 int typeId = qRegisterMetaType<QPair<type1, type2>>("QPair<" #type1 "," #type2 ">"); \
79 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToPair<type1, type2>); \
80 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertPairToPython<type1, type2>); \
81 }

◆ PythonQtRegisterToolClassesTemplateConverter

#define PythonQtRegisterToolClassesTemplateConverter ( innertype)
Value:
PythonQtRegisterListTemplateConverter(QVector, innertype); \
PythonQtRegisterListTemplateConverter(std::vector, innertype);
#define PythonQtRegisterListTemplateConverter(type, innertype)

Definition at line 102 of file PythonQtConversion.h.

102#define PythonQtRegisterToolClassesTemplateConverter(innertype) \
103 PythonQtRegisterListTemplateConverter(QList, innertype); \
104 PythonQtRegisterListTemplateConverter(QVector, innertype); \
105 PythonQtRegisterListTemplateConverter(std::vector, innertype);

◆ PythonQtRegisterToolClassesTemplateConverterForKnownClass

#define PythonQtRegisterToolClassesTemplateConverterForKnownClass ( innertype)
Value:
PythonQtRegisterListTemplateConverterForKnownClass(QVector, innertype); \
PythonQtRegisterListTemplateConverterForKnownClass(std::vector, innertype);
#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype)

Definition at line 107 of file PythonQtConversion.h.

107#define PythonQtRegisterToolClassesTemplateConverterForKnownClass(innertype) \
108 PythonQtRegisterListTemplateConverterForKnownClass(QList, innertype); \
109 PythonQtRegisterListTemplateConverterForKnownClass(QVector, innertype); \
110 PythonQtRegisterListTemplateConverterForKnownClass(std::vector, innertype);

Typedef Documentation

◆ PythonQtConvertMetaTypeToPythonCB

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

Definition at line 54 of file PythonQtConversion.h.

◆ PythonQtConvertPythonSequenceToQVariantListCB

typedef QVariant PythonQtConvertPythonSequenceToQVariantListCB(PyObject *inObject)

Definition at line 56 of file PythonQtConversion.h.

◆ PythonQtConvertPythonToMetaTypeCB

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

Definition at line 55 of file PythonQtConversion.h.

Function Documentation

◆ PythonQtConvertIntegerMapToPython()

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

Definition at line 524 of file PythonQtConversion.h.

525{
526 MapType* map = (MapType*)inMap;
527 static int innerType = -1;
528 if (innerType == -1) {
529 QByteArray innerTypes =
531 QList<QByteArray> names = innerTypes.split(',');
532 innerType = PythonQtUtils::metaTypeIdFromTypeName(names.at(1).trimmed());
533 }
534 if (innerType == QMetaType::UnknownType) {
535 std::cerr << "PythonQtConvertIntegerMapToPython: unknown inner type "
536 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
537 }
538
539 PyObject* result = PyDict_New();
540 typename MapType::const_iterator t = map->constBegin();
541 PyObject* key;
542 PyObject* val;
543 for (; t != map->constEnd(); t++) {
544 key = PyLong_FromLong(t.key());
545 val = PythonQtConv::convertQtValueToPythonInternal(innerType, &t.value());
546 PyDict_SetItem(result, key, val);
547 Py_DECREF(key);
548 Py_DECREF(val);
549 }
550 return result;
551}
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,...
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject<InnerType>
const char * typeNameFromMetaTypeId(int metaTypeId)
Returns the type name from a meta type ID.
int metaTypeIdFromTypeName(const QByteArray &className)
Returns the meta type ID from a type name.

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

◆ PythonQtConvertListOfKnownClassToPythonList()

template<class ListType, class T>
PyObject * PythonQtConvertListOfKnownClassToPythonList ( const void * inList,
int metaTypeId )

Definition at line 328 of file PythonQtConversion.h.

329{
330 ListType* list = (ListType*)inList;
331 static PythonQtClassInfo* innerType = PythonQt::priv()->getClassInfo(
333 if (innerType == nullptr) {
334 std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type for "
335 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
336 }
337 PyObject* result = PyTuple_New(list->size());
338 int i = 0;
339 for (const T& value : *list) {
340 T* newObject = new T(value);
342 (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newObject, innerType->className());
343 wrap->_ownedByPythonQt = true;
344 PyTuple_SET_ITEM(result, i, (PyObject*)wrap);
345 i++;
346 }
347 return result;
348}
a class that stores all required information about a Qt object (and an optional associated C++ class ...
const QByteArray & className() const
get the classname (either of the QObject or of the wrapped CPP object)
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)
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:572
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...
bool _ownedByPythonQt
flag that stores if the object is owned by pythonQt

References PythonQtInstanceWrapper::_ownedByPythonQt, PythonQtClassInfo::className(), PythonQtPrivate::getClassInfo(), PythonQtMethodInfo::getInnerListTypeName(), PythonQt::priv(), and PythonQtUtils::typeNameFromMetaTypeId().

◆ PythonQtConvertListOfPairToPythonList()

template<class ListType, class T1, class T2>
PyObject * PythonQtConvertListOfPairToPythonList ( const void * inList,
int metaTypeId )

Definition at line 467 of file PythonQtConversion.h.

468{
469 ListType* list = (ListType*)inList;
470 static int innerType =
472 if (innerType == QMetaType::UnknownType) {
473 std::cerr << "PythonQtConvertListOfPairToPythonList: unknown inner type "
474 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
475 }
476 PyObject* result = PyTuple_New(list->size());
477 int i = 0;
478 typedef const QPair<T1, T2> Pair;
479 for (Pair& value : *list) {
480 PyObject* object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
481 PyTuple_SET_ITEM(result, i, object);
482 i++;
483 }
484 return result;
485}
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject<InnerType>

References PythonQtMethodInfo::getInnerTemplateMetaType(), PythonQtConvertPairToPython(), and PythonQtUtils::typeNameFromMetaTypeId().

◆ PythonQtConvertListOfValueTypeToPythonList()

template<class ListType, class T>
PyObject * PythonQtConvertListOfValueTypeToPythonList ( const void * inList,
int metaTypeId )

Definition at line 273 of file PythonQtConversion.h.

274{
275 ListType* list = (ListType*)inList;
276 static const int innerType =
278 if (innerType == QMetaType::UnknownType) {
279 std::cerr << "PythonQtConvertListOfValueTypeToPythonList: unknown inner type "
280 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
281 }
282 PyObject* result = PyTuple_New(list->size());
283 int i = 0;
284 for (const T& value : *list) {
285 PyTuple_SET_ITEM(result, i, PythonQtConv::convertQtValueToPythonInternal(innerType, &value));
286 i++;
287 }
288 return result;
289}

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

◆ PythonQtConvertPairToPython()

template<class T1, class T2>
PyObject * PythonQtConvertPairToPython ( const void * inPair,
int metaTypeId )

Definition at line 394 of file PythonQtConversion.h.

395{
396 QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
397 static int innerType1 = -1;
398 static int innerType2 = -1;
399 if (innerType1 == -1) {
400 QByteArray innerTypes =
402 QList<QByteArray> names = innerTypes.split(',');
403 innerType1 = PythonQtUtils::metaTypeIdFromTypeName(names.at(0).trimmed());
404 innerType2 = PythonQtUtils::metaTypeIdFromTypeName(names.at(1).trimmed());
405 }
406 if (innerType1 == QMetaType::UnknownType || innerType2 == QMetaType::UnknownType) {
407 std::cerr << "PythonQtConvertPairToPython: unknown inner type " << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId)
408 << std::endl;
409 }
410 PyObject* result = PyTuple_New(2);
411 PyTuple_SET_ITEM(result, 0, PythonQtConv::convertQtValueToPythonInternal(innerType1, &pair->first));
412 PyTuple_SET_ITEM(result, 1, PythonQtConv::convertQtValueToPythonInternal(innerType2, &pair->second));
413 return result;
414}

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

Referenced by PythonQtConvertListOfPairToPythonList().

◆ PythonQtConvertPythonListToListOfKnownClass()

template<class ListType, class T>
bool PythonQtConvertPythonListToListOfKnownClass ( PyObject * obj,
void * outList,
int metaTypeId,
bool  )

Definition at line 351 of file PythonQtConversion.h.

353{
354 ListType* list = (ListType*)outList;
355 static PythonQtClassInfo* innerType = PythonQt::priv()->getClassInfo(
357 if (innerType == nullptr) {
358 std::cerr << "PythonQtConvertListOfKnownClassToPythonList: unknown inner type for "
359 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
360 }
361 bool result = false;
362 if (PySequence_Check(obj)) {
363 int count = PySequence_Size(obj);
364 if (count >= 0) {
365 result = true;
366 PyObject* value;
367 for (int i = 0; i < count; i++) {
368 value = PySequence_GetItem(obj, i);
369 if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
371 bool ok;
372 T* object = (T*)PythonQtConv::castWrapperTo(wrap, innerType->className(), ok);
373 Py_XDECREF(value);
374 if (ok) {
375 list->push_back(*object);
376 } else {
377 result = false;
378 break;
379 }
380 } else {
381 Py_XDECREF(value);
382 result = false;
383 break;
384 }
385 }
386 }
387 }
388 return result;
389}
PYTHONQT_EXPORT PyTypeObject PythonQtInstanceWrapper_Type
static void * castWrapperTo(PythonQtInstanceWrapper *wrapper, const QByteArray &className, bool &ok)
cast wrapper to given className if possible

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

◆ PythonQtConvertPythonListToListOfPair()

template<class ListType, class T1, class T2>
bool PythonQtConvertPythonListToListOfPair ( PyObject * obj,
void * outList,
int metaTypeId,
bool  )

Definition at line 488 of file PythonQtConversion.h.

490{
491 ListType* list = (ListType*)outList;
492 static int innerType =
494 if (innerType == QMetaType::UnknownType) {
495 std::cerr << "PythonQtConvertPythonListToListOfPair: unknown inner type "
496 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
497 }
498 bool result = false;
499 if (PySequence_Check(obj)) {
500 int count = PySequence_Size(obj);
501 if (count >= 0) {
502 result = true;
503 PyObject* value;
504 for (int i = 0; i < count; i++) {
505 QPair<T1, T2> pair;
506 value = PySequence_GetItem(obj, i);
507 if (PythonQtConvertPythonToPair<T1, T2>(value, &pair, innerType, false)) {
508 Py_XDECREF(value);
509 list->push_back(pair);
510 } else {
511 Py_XDECREF(value);
512 result = false;
513 break;
514 }
515 }
516 }
517 }
518 return result;
519}

References PythonQtMethodInfo::getInnerTemplateMetaType(), PythonQtConvertPythonToPair(), and PythonQtUtils::typeNameFromMetaTypeId().

◆ PythonQtConvertPythonListToListOfValueType()

template<class ListType, class T>
bool PythonQtConvertPythonListToListOfValueType ( PyObject * obj,
void * outList,
int metaTypeId,
bool  )

Definition at line 292 of file PythonQtConversion.h.

294{
295 ListType* list = (ListType*)outList;
296 static const int innerType =
298 if (innerType == QMetaType::UnknownType) {
299 std::cerr << "PythonQtConvertPythonListToListOfValueType: unknown inner type "
300 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
301 }
302 bool result = false;
303 if (PySequence_Check(obj)) {
304 int count = PySequence_Size(obj);
305 if (count >= 0) {
306 result = true;
307 PyObject* value;
308 for (int i = 0; i < count; i++) {
309 value = PySequence_GetItem(obj, i);
310 // this is quite some overhead, but it avoids having another large switch...
311 QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
312 Py_XDECREF(value);
313 if (v.isValid()) {
314 list->push_back(qvariant_cast<T>(v));
315 } else {
316 result = false;
317 break;
318 }
319 }
320 }
321 }
322 return result;
323}
static QVariant PyObjToQVariant(PyObject *val, int type=-1)

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

◆ PythonQtConvertPythonToIntegerMap()

template<class MapType, class T>
bool PythonQtConvertPythonToIntegerMap ( PyObject * val,
void * outMap,
int metaTypeId,
bool  )

Definition at line 554 of file PythonQtConversion.h.

555{
556 MapType* map = (MapType*)outMap;
557 static int innerType = -1;
558 if (innerType == -1) {
559 QByteArray innerTypes =
561 QList<QByteArray> names = innerTypes.split(',');
562 innerType = PythonQtUtils::metaTypeIdFromTypeName(names.at(1).trimmed());
563 }
564 if (innerType == QMetaType::UnknownType) {
565 std::cerr << "PythonQtConvertPythonToIntegerMap: unknown inner type "
566 << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId) << std::endl;
567 }
568 bool result = false;
569 if (PyMapping_Check(val)) {
570 result = true;
571 PyObject* items = PyMapping_Items(val);
572 if (items) {
573 int count = PyList_Size(items);
574 PyObject* value;
575 PyObject* key;
576 PyObject* tuple;
577 for (int i = 0; i < count; i++) {
578 tuple = PyList_GetItem(items, i);
579 key = PyTuple_GetItem(tuple, 0);
580 value = PyTuple_GetItem(tuple, 1);
581
582 bool ok;
583 int intKey = PythonQtConv::PyObjGetInt(key, false, ok);
584 // this is quite some overhead, but it avoids having another large switch...
585 QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
586 if (v.isValid() && ok) {
587 map->insert(intKey, qvariant_cast<T>(v));
588 } else {
589 result = false;
590 break;
591 }
592 }
593 Py_DECREF(items);
594 }
595 }
596 return result;
597}
static int PyObjGetInt(PyObject *val, bool strict, bool &ok)
get int from py object

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

◆ PythonQtConvertPythonToPair()

template<class T1, class T2>
bool PythonQtConvertPythonToPair ( PyObject * obj,
void * outPair,
int metaTypeId,
bool  )

Definition at line 417 of file PythonQtConversion.h.

418{
419 QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
420 static int innerType1 = -1;
421 static int innerType2 = -1;
422 if (innerType1 == -1) {
423 QByteArray innerTypes =
425 QList<QByteArray> names = innerTypes.split(',');
426 innerType1 = PythonQtUtils::metaTypeIdFromTypeName(names.at(0).trimmed());
427 innerType2 = PythonQtUtils::metaTypeIdFromTypeName(names.at(1).trimmed());
428 }
429 if (innerType1 == QMetaType::UnknownType || innerType2 == QMetaType::UnknownType) {
430 std::cerr << "PythonQtConvertPythonToPair: unknown inner type " << PythonQtUtils::typeNameFromMetaTypeId(metaTypeId)
431 << std::endl;
432 }
433 bool result = false;
434 if (PySequence_Check(obj)) {
435 int count = PySequence_Size(obj);
436 if (count == 2) {
437 result = true;
438 PyObject* value;
439
440 value = PySequence_GetItem(obj, 0);
441 // this is quite some overhead, but it avoids having another large switch...
442 QVariant v = PythonQtConv::PyObjToQVariant(value, innerType1);
443 Py_XDECREF(value);
444 if (v.isValid()) {
445 pair->first = qvariant_cast<T1>(v);
446 } else {
447 return false;
448 }
449
450 value = PySequence_GetItem(obj, 1);
451 // this is quite some overhead, but it avoids having another large switch...
452 v = PythonQtConv::PyObjToQVariant(value, innerType2);
453 Py_XDECREF(value);
454 if (v.isValid()) {
455 pair->second = qvariant_cast<T2>(v);
456 } else {
457 return false;
458 }
459 }
460 }
461 return result;
462}

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

Referenced by PythonQtConvertPythonListToListOfPair().