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);
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;
236 template<
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) {
253 template<
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));
286 template<
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);
306 template<
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);
346 template<
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;
367 template<
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);
415 template<
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) {
427 PyObject*
object = PythonQtConvertPairToPython<T1, T2>(&value, innerType);
428 PyTuple_SET_ITEM(result, i,
object);
434 template<
class ListType,
class T1,
class T2>
437 ListType* list = (ListType*)outList;
439 if (innerType == QVariant::Invalid) {
440 std::cerr <<
"PythonQtConvertPythonListToListOfPair: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
443 if (PySequence_Check(obj)) {
444 int count = PySequence_Size(obj);
448 for (
int i = 0; i < count; i++) {
450 value = PySequence_GetItem(obj, i);
451 if (PythonQtConvertPythonToPair<T1,T2>(value, &pair, innerType,
false)) {
453 list->push_back(pair);
467 template<
class MapType,
class T>
470 MapType* map = (MapType*)inMap;
471 static int innerType = -1;
472 if (innerType == -1) {
474 QList<QByteArray> names = innerTypes.split(
',');
475 innerType = QMetaType::type(names.at(1).trimmed());
477 if (innerType == QVariant::Invalid) {
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++) {
486 key = PyInt_FromLong(t.key());
488 PyDict_SetItem(result, key, val);
495 template<
class MapType,
class T>
498 MapType* map = (MapType*)outMap;
499 static int innerType = -1;
500 if (innerType == -1) {
502 QList<QByteArray> names = innerTypes.split(
',');
503 innerType = QMetaType::type(names.at(1).trimmed());
505 if (innerType == QVariant::Invalid) {
506 std::cerr <<
"PythonQtConvertPythonToIntegerMap: unknown inner type " <<
QMetaType::typeName(metaTypeId) << std::endl;
509 if (PyMapping_Check(val)) {
511 PyObject* items = PyMapping_Items(val);
513 int count = PyList_Size(items);
517 for (
int i = 0; i < count; i++) {
518 tuple = PyList_GetItem(items, i);
519 key = PyTuple_GetItem(tuple, 0);
520 value = PyTuple_GetItem(tuple, 1);
526 if (v.isValid() && ok) {
527 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 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 int stringRefTypeId
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 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 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 * 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 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)
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