1#ifndef _PYTHONQTCONVERSION_H
2#define _PYTHONQTCONVERSION_H
58#define PythonQtRegisterListTemplateConverter(type, innertype) \
59{ int typeId = qRegisterMetaType<type<innertype > >(#type"<"#innertype">"); \
60PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfValueType<type<innertype >, innertype>); \
61PythonQtConv::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);
190#if QT_VERSION < 0x060000
193 static PyObject* convertFromStringView(
const void* inObject,
int );
194 static PyObject* convertFromAnyStringView(
const void* inObject,
int );
195 static PyObject* convertFromByteArrayView(
const void* inObject,
int );
221 template <
typename Map>
224 template <
typename Map>
227#if QT_VERSION < 0x060000
230 static int stringViewTypeId;
231 static int anyStringViewTypeId;
232 static int byteArrayViewTypeId;
236template<
class ListType,
class T>
239 ListType* list = (ListType*)inList;
241 if (innerType == QVariant::Invalid) {
242 std::cerr <<
"PythonQtConvertListOfValueTypeToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
244 PyObject* result = PyTuple_New(list->size());
246 Q_FOREACH (
const T& value, *list) {
253template<
class ListType,
class T>
256 ListType* list = (ListType*)outList;
258 if (innerType == QVariant::Invalid) {
259 std::cerr <<
"PythonQtConvertPythonListToListOfValueType: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
262 if (PySequence_Check(obj)) {
263 int count = PySequence_Size(obj);
267 for (
int i = 0;i<count;i++) {
268 value = PySequence_GetItem(obj,i);
273 list->push_back(qvariant_cast<T>(v));
286template<
class ListType,
class T>
289 ListType* list = (ListType*)inList;
291 if (innerType ==
nullptr) {
292 std::cerr <<
"PythonQtConvertListOfKnownClassToPythonList: unknown inner type for " << QMetaType::typeName(metaTypeId) << std::endl;
294 PyObject* result = PyTuple_New(list->size());
296 Q_FOREACH(
const T& value, *list) {
297 T* newObject =
new T(value);
300 PyTuple_SET_ITEM(result, i, (
PyObject*)wrap);
306template<
class ListType,
class T>
309 ListType* list = (ListType*)outList;
311 if (innerType ==
nullptr) {
312 std::cerr <<
"PythonQtConvertListOfKnownClassToPythonList: unknown inner type for " << QMetaType::typeName(metaTypeId) << std::endl;
315 if (PySequence_Check(obj)) {
316 int count = PySequence_Size(obj);
320 for (
int i = 0; i < count; i++) {
321 value = PySequence_GetItem(obj, i);
328 list->push_back(*
object);
346template<
class T1,
class T2>
349 QPair<T1, T2>* pair = (QPair<T1, T2>*)inPair;
350 static int innerType1 = -1;
351 static int innerType2 = -1;
352 if (innerType1==-1) {
354 QList<QByteArray> names = innerTypes.split(
',');
355 innerType1 = QMetaType::type(names.at(0).trimmed());
356 innerType2 = QMetaType::type(names.at(1).trimmed());
358 if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
359 std::cerr <<
"PythonQtConvertPairToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
367template<
class T1,
class T2>
370 QPair<T1, T2>* pair = (QPair<T1, T2>*)outPair;
371 static int innerType1 = -1;
372 static int innerType2 = -1;
373 if (innerType1 == -1) {
375 QList<QByteArray> names = innerTypes.split(
',');
376 innerType1 = QMetaType::type(names.at(0).trimmed());
377 innerType2 = QMetaType::type(names.at(1).trimmed());
379 if (innerType1 == QVariant::Invalid || innerType2 == QVariant::Invalid) {
380 std::cerr <<
"PythonQtConvertPythonToPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
383 if (PySequence_Check(obj)) {
384 int count = PySequence_Size(obj);
389 value = PySequence_GetItem(obj, 0);
394 pair->first = qvariant_cast<T1>(v);
399 value = PySequence_GetItem(obj, 1);
404 pair->second = qvariant_cast<T2>(v);
415template<
class ListType,
class T1,
class T2>
418 ListType* list = (ListType*)inList;
420 if (innerType == QVariant::Invalid) {
421 std::cerr <<
"PythonQtConvertListOfPairToPythonList: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
423 PyObject* result = PyTuple_New(list->size());
425 typedef const QPair<T1, T2> Pair;
426 Q_FOREACH(Pair& value, *list) {
434template<
class ListType,
class T1,
class T2>
440 std::cerr <<
"PythonQtConvertPythonListToListOfPair: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
467template<
class MapType,
class T>
478 std::cerr <<
"PythonQtConvertIntegerMapToPython: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
482 typename MapType::const_iterator
t =
map->constBegin();
485 for (;
t !=
map->constEnd();
t++) {
495template<
class MapType,
class T>
506 std::cerr <<
"PythonQtConvertPythonToIntegerMap: unknown inner type " << QMetaType::typeName(metaTypeId) << std::endl;
526 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)
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