1#ifndef _PYTHONQTCONVERSION_H
2#define _PYTHONQTCONVERSION_H
58#define PythonQtRegisterListTemplateConverter(type, innertype) \
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>); \
67#define PythonQtRegisterListTemplateConverterForKnownClass(type, innertype) \
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>); \
76#define PythonQtRegisterQPairConverter(type1, type2) \
78 int typeId = qRegisterMetaType<QPair<type1, type2>>("QPair<" #type1 "," #type2 ">"); \
79 PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonToPair<type1, type2>); \
80 PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertPairToPython<type1, type2>); \
83#define PythonQtRegisterIntegerMapConverter(type, innertype) \
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>); \
92#define PythonQtRegisterListTemplateQPairConverter(listtype, type1, type2) \
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>); \
102#define PythonQtRegisterToolClassesTemplateConverter(innertype) \
103 PythonQtRegisterListTemplateConverter(QList, innertype); \
104 PythonQtRegisterListTemplateConverter(QVector, innertype); \
105 PythonQtRegisterListTemplateConverter(std::vector, innertype);
107#define PythonQtRegisterToolClassesTemplateConverterForKnownClass(innertype) \
108 PythonQtRegisterListTemplateConverterForKnownClass(QList, innertype); \
109 PythonQtRegisterListTemplateConverterForKnownClass(QVector, innertype); \
110 PythonQtRegisterListTemplateConverterForKnownClass(std::vector, innertype);
147 QString s = PyObjGetString(val,
false, ok);
187 _pythonToMetaTypeConverters.insert(metaTypeId, cb);
193 _metaTypeToPythonConverters.insert(metaTypeId, cb);
200 _pythonSequenceToQVariantListCB = cb;
222#if QT_VERSION < 0x060000
225 static PyObject* convertFromStringView(
const void* inObject,
int );
226 static PyObject* convertFromAnyStringView(
const void* inObject,
int );
227 static PyObject* convertFromByteArrayView(
const void* inObject,
int );
256 template<
typename Map>
259 template<
typename Map>
262#if QT_VERSION < 0x060000
265 static int stringViewTypeId;
266 static int anyStringViewTypeId;
267 static int byteArrayViewTypeId;
271template<
class ListType,
class T>
274 ListType* list = (ListType*)inList;
275 static const int innerType =
277 if (innerType == QMetaType::UnknownType) {
278 std::cerr <<
"PythonQtConvertListOfValueTypeToPythonList: unknown inner type "
281 PyObject* result = PyTuple_New(list->size());
283 for (
const T& value : *list) {
290template<
class ListType,
class T>
294 ListType* list = (ListType*)outList;
295 static const int innerType =
297 if (innerType == QMetaType::UnknownType) {
298 std::cerr <<
"PythonQtConvertPythonListToListOfValueType: unknown inner type "
302 if (PySequence_Check(obj)) {
303 int count = PySequence_Size(obj);
307 for (
int i = 0; i < count; i++) {
308 value = PySequence_GetItem(obj, i);
313 list->push_back(qvariant_cast<T>(v));
326template<
class ListType,
class T>
329 ListType* list = (ListType*)inList;
332 if (innerType ==
nullptr) {
333 std::cerr <<
"PythonQtConvertListOfKnownClassToPythonList: unknown inner type for "
336 PyObject* result = PyTuple_New(list->size());
338 for (
const T& value : *list) {
339 T* newObject =
new T(value);
343 PyTuple_SET_ITEM(result, i, (
PyObject*)wrap);
349template<
class ListType,
class T>
353 ListType* list = (ListType*)outList;
356 if (innerType ==
nullptr) {
357 std::cerr <<
"PythonQtConvertListOfKnownClassToPythonList: unknown inner type for "
361 if (PySequence_Check(obj)) {
362 int count = PySequence_Size(obj);
366 for (
int i = 0; i < count; i++) {
367 value = PySequence_GetItem(obj, i);
374 list->push_back(*
object);
392template<
class T1,
class T2>
395 QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
396 static int innerType1 = -1;
397 static int innerType2 = -1;
398 if (innerType1 == -1) {
399 QByteArray innerTypes =
401 QList<QByteArray> names = innerTypes.split(
',');
405 if (innerType1 == QMetaType::UnknownType || innerType2 == QMetaType::UnknownType) {
415template<
class T1,
class T2>
418 QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
419 static int innerType1 = -1;
420 static int innerType2 = -1;
421 if (innerType1 == -1) {
422 QByteArray innerTypes =
424 QList<QByteArray> names = innerTypes.split(
',');
428 if (innerType1 == QMetaType::UnknownType || innerType2 == QMetaType::UnknownType) {
433 if (PySequence_Check(obj)) {
434 int count = PySequence_Size(obj);
439 value = PySequence_GetItem(obj, 0);
444 pair->first = qvariant_cast<T1>(v);
449 value = PySequence_GetItem(obj, 1);
454 pair->second = qvariant_cast<T2>(v);
465template<
class ListType,
class T1,
class T2>
468 ListType* list = (ListType*)inList;
469 static int innerType =
471 if (innerType == QMetaType::UnknownType) {
472 std::cerr <<
"PythonQtConvertListOfPairToPythonList: unknown inner type "
475 PyObject* result = PyTuple_New(list->size());
477 typedef const QPair<T1, T2> Pair;
478 for (Pair& value : *list) {
486template<
class ListType,
class T1,
class T2>
493 if (
innerType == QMetaType::UnknownType) {
494 std::cerr <<
"PythonQtConvertPythonListToListOfPair: unknown inner type "
522template<
class MapType,
class T>
533 if (
innerType == QMetaType::UnknownType) {
534 std::cerr <<
"PythonQtConvertIntegerMapToPython: unknown inner type "
539 typename MapType::const_iterator
t =
map->constBegin();
542 for (;
t !=
map->constEnd();
t++) {
552template<
class MapType,
class T>
563 if (
innerType == QMetaType::UnknownType) {
564 std::cerr <<
"PythonQtConvertPythonToIntegerMap: unknown inner type "
585 if (
v.isValid() &&
ok) {
QVariant PythonQtConvertPythonSequenceToQVariantListCB(PyObject *inObject)
PyObject * PythonQtConvertPairToPython(const void *inPair, int metaTypeId)
PyObject * PythonQtConvertIntegerMapToPython(const void *inMap, int metaTypeId)
PyObject * PythonQtConvertMetaTypeToPythonCB(const void *inObject, int metaTypeId)
bool PythonQtConvertPythonToIntegerMap(PyObject *val, void *outMap, int metaTypeId, bool)
bool PythonQtConvertPythonListToListOfPair(PyObject *obj, void *outList, int metaTypeId, bool)
bool PythonQtConvertPythonToMetaTypeCB(PyObject *inObject, void *outObject, int metaTypeId, bool strict)
bool PythonQtConvertPythonToPair(PyObject *obj, void *outPair, int metaTypeId, bool)
bool PythonQtConvertPythonListToListOfKnownClass(PyObject *obj, void *outList, int metaTypeId, bool)
PyObject * PythonQtConvertListOfValueTypeToPythonList(const void *inList, int metaTypeId)
bool PythonQtConvertPythonListToListOfValueType(PyObject *obj, void *outList, int metaTypeId, bool)
PyObject * PythonQtConvertListOfPairToPythonList(const void *inList, int metaTypeId)
PyObject * PythonQtConvertListOfKnownClassToPythonList(const void *inList, int metaTypeId)
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 PyObject * QVariantHashToPyObject(const QVariantHash &m)
static PyObject * QVariantMapToPyObject(const QVariantMap &m)
static void * ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo &info, PyObject *obj, bool strict, PythonQtClassInfo *classInfo, void *alreadyAllocatedCPPObject, PythonQtArgumentFrame *frame=nullptr)
convert python object to Qt (according to the given parameter) and if the conversion should be strict...
static bool isStringType(PyTypeObject *type)
Returns if the given object is a string (or unicode string)
static qint64 PyObjGetLongLong(PyObject *val, bool strict, bool &ok)
get int64 from py object
static QHash< int, PythonQtConvertPythonToMetaTypeCB * > _pythonToMetaTypeConverters
static bool convertToQListOfPythonQtObjectPtr(PyObject *obj, void *outList, int, bool)
static void setPythonSequenceToQVariantListCallback(PythonQtConvertPythonSequenceToQVariantListCB *cb)
static PyObject * mapToPython(const Map &m)
helper template function for QVariantMapToPyObject/QVariantHashToPyObject
static PyObject * convertFromPythonQtSafeObjectPtr(const void *inObject, int)
static double PyObjGetDouble(PyObject *val, bool strict, bool &ok)
get double from py object
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 bool convertToPythonQtSafeObjectPtr(PyObject *obj, void *outPtr, int, bool)
static PyObject * QStringListToPyList(const QStringList &list)
converts QStringList to Python list
static QVariant PyObjToQVariant(PyObject *val, int type=-1)
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 int stringRefTypeId
static PyObject * GetPyBool(bool val)
get a ref counted True or False Python object
static PyObject * QStringToPyObject(const QString &str)
converts QString to Python string (unicode!)
static QString PyObjGetString(PyObject *val)
get string value from py object
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 PyObject * ConvertQListOfPointerTypeToPythonList(QList< void * > *list, const PythonQtMethodInfo::ParameterInfo &info)
converts the list of pointers of given type to Python
static void * handlePythonToQtAutoConversion(int typeId, PyObject *obj, void *alreadyAllocatedCPPObject, PythonQtArgumentFrame *frame)
handle automatic conversion of some special types (QColor, QBrush, ...)
static QByteArray PyObjGetBytesAllowString(PyObject *val, bool strict, bool &ok)
get bytes from py object, also allows Python string
static QString PyObjGetString(PyObject *val, bool strict, bool &ok)
get string value from py object
static PyObject * convertFromPythonQtObjectPtr(const void *inObject, int)
static void pythonToMapVariant(PyObject *val, QVariant &result)
helper template method for conversion from Python to QVariantMap/Hash
static PyObject * QStringListToPyObject(const QStringList &list)
converts QStringList to Python tuple
static PyObject * convertFromStringRef(const void *inObject, int)
static PyObject * createCopyFromMetaType(int type, const void *object)
creates a copy of given object, using the QMetaType
static int PyObjGetInt(PyObject *val, bool strict, bool &ok)
get int from py object
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 PythonQtConvertPythonSequenceToQVariantListCB * _pythonSequenceToQVariantListCB
static PyObject * QVariantToPyObject(const QVariant &v)
convert QVariant from PyObject
static QString PyObjGetRepresentation(PyObject *val)
get string representation of py object
static void * castWrapperTo(PythonQtInstanceWrapper *wrapper, const QByteArray &className, bool &ok)
cast wrapper to given className if possible
static QHash< int, PythonQtConvertMetaTypeToPythonCB * > _metaTypeToPythonConverters
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 void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB *cb)
register a converter callback from python to cpp for given metatype
static PyObject * convertFromQListOfPythonQtObjectPtr(const void *inObject, int)
static PyObject * QVariantListToPyObject(const QVariantList &l)
static QStringList PyObjToStringList(PyObject *val, bool strict, bool &ok)
create a string list from python sequence
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB *cb)
register a converter callback from cpp to python for given metatype
static void registerStringViewTypes()
Register QStringView like types, that need to be handled specially.
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)
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.
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