1 #ifndef _PYTHONQTCONVERSION_H
2 #define _PYTHONQTCONVERSION_H
58 #define PythonQtRegisterListTemplateConverter(type, innertype) \
59 { int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
60 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfValueType<type<innertype >, innertype>); \
61 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfValueTypeToPythonList<type<innertype >, innertype>); \
64 #define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype) \
65 { int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
66 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfKnownClass<type<innertype >, innertype>); \
67 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfKnownClassToPythonList<type<innertype >, innertype>); \
70 #define PythonQtRegisterQPairConverter(type1, type2) \
71 { int typeId = qRegisterMetaType<QPair<type1, type2> >("QPair<"#type1","#type2">"); \
72 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToPair<type1, type2>); \
73 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertPairToPython<type1, type2>); \
76 #define PythonQtRegisterIntegerMapConverter(type, innertype) \
77 { int typeId = qRegisterMetaType<type<int, innertype > >(#type"<int, "#innertype">"); \
78 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToIntegerMap<type<int, innertype >, innertype>); \
79 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertIntegerMapToPython<type<int, innertype >, innertype>); \
82 #define PythonQtRegisterListTemplateQPairConverter(listtype, type1, type2) \
84 qRegisterMetaType<QPair<type1, type2> >("QPair<"#type1","#type2">"); \
85 int typeId = qRegisterMetaType<listtype<QPair<type1, type2> > >(#listtype"<QPair<"#type1","#type2">>"); \
86 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfPair<listtype<QPair<type1, type2> >, type1, type2>); \
87 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfPairToPythonList<listtype<QPair<type1, type2> >, type1, type2>); \
90 #define PythonQtRegisterToolClassesTemplateConverter(innertype) \
91 PythonQtRegisterListTemplateConverter(QList, innertype); \
92 PythonQtRegisterListTemplateConverter(QVector, innertype); \
93 PythonQtRegisterListTemplateConverter(std::vector, innertype);
95 #define PythonQtRegisterToolClassesTemplateConverterForKnownClass(innertype) \
96 PythonQtRegisterListTemplateConverterForKnownClass(QList, innertype); \
97 PythonQtRegisterListTemplateConverterForKnownClass(QVector, innertype); \
98 PythonQtRegisterListTemplateConverterForKnownClass(std::vector, innertype);
210 template <
typename Map>
213 template <
typename Map>
218 template<
class ListType,
class T>
221 ListType* list = (ListType*)inList;
223 if (innerType == QVariant::Invalid) {
224 std::cerr <<
"PythonQtConvertListOfValueTypeToPythonList: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
226 PyObject* result = PyTuple_New(list->size());
228 Q_FOREACH (
const T& value, *list) {
235 template<
class ListType,
class T>
238 ListType* list = (ListType*)outList;
240 if (innerType == QVariant::Invalid) {
241 std::cerr <<
"PythonQtConvertPythonListToListOfValueType: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
244 if (PySequence_Check(obj)) {
245 int count = PySequence_Size(obj);
249 for (
int i = 0;i<count;i++) {
250 value = PySequence_GetItem(obj,i);
255 list->push_back(qvariant_cast<T>(v));
268 template<
class ListType,
class T>
271 ListType* list = (ListType*)inList;
273 if (innerType == NULL) {
274 std::cerr <<
"PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->
className().constData() << std::endl;
276 PyObject* result = PyTuple_New(list->size());
278 Q_FOREACH(
const T& value, *list) {
279 T* newObject =
new T(value);
282 PyTuple_SET_ITEM(result, i, (
PyObject*)wrap);
288 template<
class ListType,
class T>
291 ListType* list = (ListType*)outList;
293 if (innerType == NULL) {
294 std::cerr <<
"PythonQtConvertListOfKnownClassToPythonList: unknown inner type " << innerType->
className().constData() << std::endl;
297 if (PySequence_Check(obj)) {
298 int count = PySequence_Size(obj);
302 for (
int i = 0; i < count; i++) {
303 value = PySequence_GetItem(obj, i);
310 list->push_back(*
object);
328 template<
class T1,
class T2>
331 QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
332 static int innerType1 = -1;
333 static int innerType2 = -1;
334 if (innerType1==-1) {
336 QList<QByteArray> names = innerTypes.split(
',');
337 innerType1 = QMetaType::type(names.at(0).trimmed());
338 innerType2 = QMetaType::type(names.at(1).trimmed());
340 if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
341 std::cerr <<
"PythonQtConvertPairToPython: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
349 template<
class T1,
class T2>
352 QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
353 static int innerType1 = -1;
354 static int innerType2 = -1;
355 if (innerType1 == -1) {
357 QList<QByteArray> names = innerTypes.split(
',');
358 innerType1 = QMetaType::type(names.at(0).trimmed());
359 innerType2 = QMetaType::type(names.at(1).trimmed());
361 if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
362 std::cerr <<
"PythonQtConvertPythonToPair: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
365 if (PySequence_Check(obj)) {
366 int count = PySequence_Size(obj);
371 value = PySequence_GetItem(obj, 0);
376 pair->first = qvariant_cast<T1>(v);
381 value = PySequence_GetItem(obj, 1);
386 pair->second = qvariant_cast<T2>(v);
397 template<
class ListType,
class T1,
class T2>
400 ListType* list = (ListType*)inList;
402 if (innerType == QVariant::Invalid) {
403 std::cerr <<
"PythonQtConvertListOfPairToPythonList: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
405 PyObject* result = PyTuple_New(list->size());
407 typedef const QPair<T1, T2> Pair;
408 Q_FOREACH(Pair& value, *list) {
409 PyObject*
object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
410 PyTuple_SET_ITEM(result, i,
object);
416 template<
class ListType,
class T1,
class T2>
419 ListType* list = (ListType*)outList;
421 if (innerType == QVariant::Invalid) {
422 std::cerr <<
"PythonQtConvertPythonListToListOfPair: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
425 if (PySequence_Check(obj)) {
426 int count = PySequence_Size(obj);
430 for (
int i = 0; i < count; i++) {
432 value = PySequence_GetItem(obj, i);
433 if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType,
false)) {
435 list->push_back(pair);
449 template<
class MapType,
class T>
452 MapType* map = (MapType*)inMap;
453 static int innerType = -1;
454 if (innerType == -1) {
456 QList<QByteArray> names = innerTypes.split(
',');
457 innerType = QMetaType::type(names.at(1).trimmed());
459 if (innerType == QVariant::Invalid) {
460 std::cerr <<
"PythonQtConvertIntegerMapToPython: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
464 typename MapType::const_iterator t = map->constBegin();
467 for (; t != map->constEnd(); t++) {
468 key = PyInt_FromLong(t.key());
470 PyDict_SetItem(result, key, val);
477 template<
class MapType,
class T>
480 MapType* map = (MapType*)outMap;
481 static int innerType = -1;
482 if (innerType == -1) {
484 QList<QByteArray> names = innerTypes.split(
',');
485 innerType = QMetaType::type(names.at(1).trimmed());
487 if (innerType == QVariant::Invalid) {
488 std::cerr <<
"PythonQtConvertPythonToIntegerMap: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
491 if (PyMapping_Check(val)) {
493 PyObject* items = PyMapping_Items(val);
495 int count = PyList_Size(items);
499 for (
int i = 0; i < count; i++) {
500 tuple = PyList_GetItem(items, i);
501 key = PyTuple_GetItem(tuple, 0);
502 value = PyTuple_GetItem(tuple, 1);
508 if (v.isValid() && ok) {
509 map->insert(intKey, qvariant_cast<T>(v));
QVariant PythonQtConvertPythonSequenceToQVariantListCB(PyObject *inObject)
PyObject * PythonQtConvertIntegerMapToPython(const void *inMap, int metaTypeId)
PyObject * PythonQtConvertListOfKnownClassToPythonList(const void *inList, int metaTypeId)
PyObject * PythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)
bool PythonQtConvertPythonToIntegerMap(PyObject *val, void *outMap, int metaTypeId, bool)
PyObject * PythonQtConvertPairToPython(const void *inPair, int metaTypeId)
bool PythonQtConvertPythonListToListOfPair(PyObject *obj, void *outList, int metaTypeId, bool)
PyObject * PythonQtConvertListOfValueTypeToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)
bool PythonQtConvertPythonToPair(PyObject *obj, void *outPair, int metaTypeId, bool)
PyObject * PythonQtConvertListOfPairToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonListToListOfKnownClass(PyObject *obj, void *outList, int metaTypeId, bool)
bool PythonQtConvertPythonListToListOfValueType(PyObject *obj, void *outList, int metaTypeId, bool)
PYTHONQT_EXPORT PyTypeObject PythonQtInstanceWrapper_Type
Stores C++ arguments for a qt_metacall (which are created when converting data from Python to C++)
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)
a static class that offers methods for type conversion
static quint64 PyObjGetULongLong(PyObject *val, bool strict, bool &ok)
get int64 from py object
static bool convertToPythonQtObjectPtr(PyObject *obj, void *outPtr, int, bool)
static void * ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo &info, PyObject *obj, bool strict, PythonQtClassInfo *classInfo, void *alreadyAllocatedCPPObject, PythonQtArgumentFrame *frame=NULL)
convert python object to Qt (according to the given parameter) and if the conversion should be strict...
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 PyObject * ConvertQListOfPointerTypeToPythonList(QList< void * > *list, const PythonQtMethodInfo::ParameterInfo &info)
converts the list of pointers of given type to Python
static bool isStringType(PyTypeObject *type)
Returns if the given object is a string (or unicode string)
static PyObject * convertFromPythonQtSafeObjectPtr(const void *inObject, int)
static qint64 PyObjGetLongLong(PyObject *val, bool strict, bool &ok)
get int64 from py object
static PyObject * createCopyFromMetaType(int type, const void *object)
creates a copy of given object, using the QMetaType
static QHash< int, PythonQtConvertPythonToMetaTypeCB * > _pythonToMetaTypeConverters
static bool convertToQListOfPythonQtObjectPtr(PyObject *obj, void *outList, int, bool)
static void setPythonSequenceToQVariantListCallback(PythonQtConvertPythonSequenceToQVariantListCB *cb)
static double PyObjGetDouble(PyObject *val, bool strict, bool &ok)
get double from py object
static void * castWrapperTo(PythonQtInstanceWrapper *wrapper, const QByteArray &className, bool &ok)
cast wrapper to given className if possible
static void * handlePythonToQtAutoConversion(int typeId, PyObject *obj, void *alreadyAllocatedCPPObject, PythonQtArgumentFrame *frame)
handle automatic conversion of some special types (QColor, QBrush, ...)
static bool convertToPythonQtSafeObjectPtr(PyObject *obj, void *outPtr, int, bool)
static QVariant PyObjToQVariant(PyObject *val, int type=-1)
static PyObject * QVariantListToPyObject(const QVariantList &l)
static void * CreateQtReturnValue(const PythonQtMethodInfo::ParameterInfo &info, PythonQtArgumentFrame *frame)
creates a data storage for the passed parameter type and returns a void pointer to be set as arg[0] o...
static PyObject * convertFromQListOfPythonQtObjectPtr(const void *inObject, int)
static PyObject * QVariantToPyObject(const QVariant &v)
convert QVariant from PyObject
static PyObject * QStringListToPyList(const QStringList &list)
converts QStringList to Python list
static PyObject * mapToPython(const Map &m)
helper template function for QVariantMapToPyObject/QVariantHashToPyObject
static QByteArray getCPPTypeName(PyObject *type)
Returns the name of the equivalent CPP type (for signals and slots)
static bool ConvertPythonListToQListOfPointerType(PyObject *obj, QList< void * > *list, const PythonQtMethodInfo::ParameterInfo &info, bool strict)
tries to convert the python object to a QList of pointers to type objects, returns true on success
static PyObject * convertFromStringRef(const void *inObject, int)
static PyObject * QVariantHashToPyObject(const QVariantHash &m)
static PyObject * QStringToPyObject(const QString &str)
converts QString to Python string (unicode!)
static QString PyObjGetString(PyObject *val)
get string value from py object
static PyObject * convertFromPythonQtObjectPtr(const void *inObject, int)
static QByteArray PyObjGetBytes(PyObject *val, bool strict, bool &ok)
get bytes from py object
static QString CPPObjectToString(int type, const void *data)
get human readable string from CPP object (when the metatype is known)
static bool PyObjGetBool(PyObject *val, bool strict, bool &ok)
get bool from py object
static QString PyObjGetString(PyObject *val, bool strict, bool &ok)
get string value from py object
static void pythonToMapVariant(PyObject *val, QVariant &result)
helper template method for conversion from Python to QVariantMap/Hash
static PyObject * QVariantMapToPyObject(const QVariantMap &m)
static PyObject * ConvertQtValueToPython(const PythonQtMethodInfo::ParameterInfo &info, const void *data)
converts the Qt parameter given in data, interpreting it as a info parameter, into a Python object,
static PyObject * GetPyBool(bool val)
get a ref counted True or False Python object
static int PyObjGetInt(PyObject *val, bool strict, bool &ok)
get int from py object
static PythonQtConvertPythonSequenceToQVariantListCB * _pythonSequenceToQVariantListCB
static QString PyObjGetRepresentation(PyObject *val)
get string representation of py object
static QHash< int, PythonQtConvertMetaTypeToPythonCB * > _metaTypeToPythonConverters
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static QStringList PyObjToStringList(PyObject *val, bool strict, bool &ok)
create a string list from python sequence
static PyObject * QStringListToPyObject(const QStringList &list)
converts QStringList to Python tuple
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject<InnerType>
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject<InnerType>
static QByteArray getInnerListTypeName(const QByteArray &typeName)
returns the inner type name of a simple template or the typename without appended "List".
PyObject * wrapPtr(void *ptr, const QByteArray &name, bool passOwnership=false)
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)
QByteArray typeName(const QMetaMethod &method)
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
stores various informations about a parameter/type name