@@ -1007,21 +1007,21 QStringList PythonQt::introspectType(const QString& typeName, ObjectType type) | |||||
1007 | return results; |
|
1007 | return results; | |
1008 | } |
|
1008 | } | |
1009 |
|
1009 | |||
1010 | QVariant PythonQt::call(PyObject* object, const QString& name, const QVariantList& args) |
|
1010 | QVariant PythonQt::call(PyObject* object, const QString& name, const QVariantList& args, const QVariantMap& kwargs) | |
1011 | { |
|
1011 | { | |
1012 | PythonQtObjectPtr callable = lookupCallable(object, name); |
|
1012 | PythonQtObjectPtr callable = lookupCallable(object, name); | |
1013 | if (callable) { |
|
1013 | if (callable) { | |
1014 | return call(callable, args); |
|
1014 | return call(callable, args, kwargs); | |
1015 | } else { |
|
1015 | } else { | |
1016 | return QVariant(); |
|
1016 | return QVariant(); | |
1017 | } |
|
1017 | } | |
1018 | } |
|
1018 | } | |
1019 |
|
1019 | |||
1020 | QVariant PythonQt::call(PyObject* callable, const QVariantList& args) |
|
1020 | QVariant PythonQt::call(PyObject* callable, const QVariantList& args, const QVariantMap& kwargs) | |
1021 | { |
|
1021 | { | |
1022 | QVariant r; |
|
1022 | QVariant r; | |
1023 | PythonQtObjectPtr result; |
|
1023 | PythonQtObjectPtr result; | |
1024 | result.setNewRef(callAndReturnPyObject(callable, args)); |
|
1024 | result.setNewRef(callAndReturnPyObject(callable, args, kwargs)); | |
1025 | if (result) { |
|
1025 | if (result) { | |
1026 | r = PythonQtConv::PyObjToQVariant(result); |
|
1026 | r = PythonQtConv::PyObjToQVariant(result); | |
1027 | } else { |
|
1027 | } else { | |
@@ -1030,17 +1030,17 QVariant PythonQt::call(PyObject* callable, const QVariantList& args) | |||||
1030 | return r; |
|
1030 | return r; | |
1031 | } |
|
1031 | } | |
1032 |
|
1032 | |||
1033 | PyObject* PythonQt::callAndReturnPyObject(PyObject* callable, const QVariantList& args) |
|
1033 | PyObject* PythonQt::callAndReturnPyObject(PyObject* callable, const QVariantList& args, const QVariantMap& kwargs) | |
1034 | { |
|
1034 | { | |
1035 | PyObject* result = NULL; |
|
1035 | PyObject* result = NULL; | |
1036 | if (callable) { |
|
1036 | if (callable) { | |
|
1037 | bool err = false; | |||
1037 | PythonQtObjectPtr pargs; |
|
1038 | PythonQtObjectPtr pargs; | |
1038 | int count = args.size(); |
|
1039 | int count = args.size(); | |
1039 | if (count>0) { |
|
1040 | if ((count > 0) || (kwargs.count() > 0)) { // create empty tuple if kwargs are given | |
1040 | pargs.setNewRef(PyTuple_New(count)); |
|
1041 | pargs.setNewRef(PyTuple_New(count)); | |
1041 | } |
|
1042 | ||
1042 | bool err = false; |
|
1043 | // transform QVariant arguments to Python | |
1043 | // transform QVariants to Python |
|
|||
1044 | for (int i = 0; i < count; i++) { |
|
1044 | for (int i = 0; i < count; i++) { | |
1045 | PyObject* arg = PythonQtConv::QVariantToPyObject(args.at(i)); |
|
1045 | PyObject* arg = PythonQtConv::QVariantToPyObject(args.at(i)); | |
1046 | if (arg) { |
|
1046 | if (arg) { | |
@@ -1051,10 +1051,33 PyObject* PythonQt::callAndReturnPyObject(PyObject* callable, const QVariantList | |||||
1051 | break; |
|
1051 | break; | |
1052 | } |
|
1052 | } | |
1053 | } |
|
1053 | } | |
1054 |
|
1054 | } | ||
1055 | if (!err) { |
|
1055 | if (!err) { | |
|
1056 | if (kwargs.isEmpty()) { | |||
|
1057 | // do a direct call if we have no keyword arguments | |||
1056 | PyErr_Clear(); |
|
1058 | PyErr_Clear(); | |
1057 | result = PyObject_CallObject(callable, pargs); |
|
1059 | result = PyObject_CallObject(callable, pargs); | |
|
1060 | } else { | |||
|
1061 | // convert keyword arguments to Python | |||
|
1062 | PythonQtObjectPtr pkwargs; | |||
|
1063 | pkwargs.setNewRef(PyDict_New()); | |||
|
1064 | QMapIterator<QString, QVariant> it(kwargs); | |||
|
1065 | while (it.hasNext()) { | |||
|
1066 | it.next(); | |||
|
1067 | PyObject* arg = PythonQtConv::QVariantToPyObject(it.value()); | |||
|
1068 | if (arg) { | |||
|
1069 | PyDict_SetItemString(pkwargs, it.key().toLatin1().constData(), arg); | |||
|
1070 | } else { | |||
|
1071 | err = true; | |||
|
1072 | break; | |||
|
1073 | } | |||
|
1074 | } | |||
|
1075 | if (!err) { | |||
|
1076 | // call with arguments and keyword arguments | |||
|
1077 | PyErr_Clear(); | |||
|
1078 | result = PyObject_Call(callable, pargs, pkwargs); | |||
|
1079 | } | |||
|
1080 | } | |||
1058 | } |
|
1081 | } | |
1059 | } |
|
1082 | } | |
1060 | return result; |
|
1083 | return result; |
@@ -356,13 +356,13 public: | |||||
356 | //@{ |
|
356 | //@{ | |
357 |
|
357 | |||
358 | //! call the given python \c callable in the scope of object, returns the result converted to a QVariant |
|
358 | //! call the given python \c callable in the scope of object, returns the result converted to a QVariant | |
359 | QVariant call(PyObject* object, const QString& callable, const QVariantList& args = QVariantList()); |
|
359 | QVariant call(PyObject* object, const QString& callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap()); | |
360 |
|
360 | |||
361 | //! call the given python object, returns the result converted to a QVariant |
|
361 | //! call the given python object, returns the result converted to a QVariant | |
362 | QVariant call(PyObject* callable, const QVariantList& args = QVariantList()); |
|
362 | QVariant call(PyObject* callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap()); | |
363 |
|
363 | |||
364 | //! call the given python object, returns the result as new PyObject |
|
364 | //! call the given python object, returns the result as new PyObject | |
365 | PyObject* callAndReturnPyObject(PyObject* callable, const QVariantList& args = QVariantList()); |
|
365 | PyObject* callAndReturnPyObject(PyObject* callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap()); | |
366 |
|
366 | |||
367 | //@} |
|
367 | //@} | |
368 |
|
368 |
@@ -96,14 +96,14 QVariant PythonQtObjectPtr::getVariable(const QString& name) | |||||
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | QVariant PythonQtObjectPtr::call(const QString& callable, const QVariantList& args) |
|
99 | QVariant PythonQtObjectPtr::call(const QString& callable, const QVariantList& args, const QVariantMap& kwargs) | |
100 | { |
|
100 | { | |
101 | return PythonQt::self()->call(_object, callable, args); |
|
101 | return PythonQt::self()->call(_object, callable, args, kwargs); | |
102 | } |
|
102 | } | |
103 |
|
103 | |||
104 | QVariant PythonQtObjectPtr::call(const QVariantList& args) |
|
104 | QVariant PythonQtObjectPtr::call(const QVariantList& args, const QVariantMap& kwargs) | |
105 | { |
|
105 | { | |
106 | return PythonQt::self()->call(_object, args); |
|
106 | return PythonQt::self()->call(_object, args, kwargs); | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | bool PythonQtObjectPtr::fromVariant(const QVariant& variant) |
|
109 | bool PythonQtObjectPtr::fromVariant(const QVariant& variant) |
@@ -47,6 +47,7 | |||||
47 | #include "PythonQtSystem.h" |
|
47 | #include "PythonQtSystem.h" | |
48 | #include <QVariant> |
|
48 | #include <QVariant> | |
49 | #include <QVariantList> |
|
49 | #include <QVariantList> | |
|
50 | #include <QVariantMap> | |||
50 |
|
51 | |||
51 | //! a smart pointer that stores a PyObject pointer and that handles reference counting automatically |
|
52 | //! a smart pointer that stores a PyObject pointer and that handles reference counting automatically | |
52 | class PYTHONQT_EXPORT PythonQtObjectPtr |
|
53 | class PYTHONQT_EXPORT PythonQtObjectPtr | |
@@ -142,10 +143,10 public: | |||||
142 | QVariant getVariable(const QString& name); |
|
143 | QVariant getVariable(const QString& name); | |
143 |
|
144 | |||
144 | //! call the given python object (in the scope of the current object), returns the result converted to a QVariant |
|
145 | //! call the given python object (in the scope of the current object), returns the result converted to a QVariant | |
145 | QVariant call(const QString& callable, const QVariantList& args = QVariantList()); |
|
146 | QVariant call(const QString& callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap()); | |
146 |
|
147 | |||
147 | //! call the contained python object directly, returns the result converted to a QVariant |
|
148 | //! call the contained python object directly, returns the result converted to a QVariant | |
148 | QVariant call(const QVariantList& args = QVariantList()); |
|
149 | QVariant call(const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap()); | |
149 |
|
150 | |||
150 | protected: |
|
151 | protected: | |
151 |
|
152 |
General Comments 0
You need to be logged in to leave comments.
Login now