PythonQt
PythonQt.h
Go to the documentation of this file.
1 #ifndef _PYTHONQT_H
2 #define _PYTHONQT_H
3 
4 /*
5  *
6  * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * Further, this software is distributed without any warranty that it is
19  * free of the rightful claim of any third person regarding infringement
20  * or the like. Any license provided herein, whether implied or
21  * otherwise, applies only to this software file. Patent licenses, if
22  * any, provided herein do not apply to combinations of this program with
23  * other software, or any other product whatsoever.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
30  * 28359 Bremen, Germany or:
31  *
32  * http://www.mevis.de
33  *
34  */
35 
36 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
44 
45 #include "PythonQtPythonInclude.h"
46 #include "PythonQtUtils.h"
47 #include "PythonQtSystem.h"
49 #include "PythonQtClassWrapper.h"
50 #include "PythonQtSlot.h"
51 #include "PythonQtObjectPtr.h"
52 #include "PythonQtStdIn.h"
53 #include "PythonQtThreadSupport.h"
54 #include <QObject>
55 #include <QVariant>
56 #include <QList>
57 #include <QHash>
58 #include <QByteArray>
59 #include <QStringList>
60 #include <QtDebug>
61 #include <iostream>
62 
63 
64 class PythonQtClassInfo;
65 class PythonQtPrivate;
66 class PythonQtMethodInfo;
72 
73 typedef void PythonQtVoidPtrCB(void* object);
74 typedef void PythonQtQObjectWrappedCB(QObject* object);
75 typedef void PythonQtQObjectNoLongerWrappedCB(QObject* object);
76 typedef void* PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name);
77 typedef QString PythonQtQObjectMissingAttributeCB(QObject* object, const QString& attribute);
78 
79 typedef void PythonQtShellSetInstanceWrapperCB(void* object, PythonQtInstanceWrapper* wrapper);
80 
81 template<class T> void PythonQtSetInstanceWrapperOnShell(void* object, PythonQtInstanceWrapper* wrapper) {
82  (reinterpret_cast<T*>(object))->_wrapper = wrapper;
83 }
84 
88 template<class T>
90 {
91  public:
93  PythonQtPassOwnershipToCPP(const T& t):_t(t) {}
95  operator T() const { return _t; }
96 
99  T _t;
100 };
101 
105 template<class T>
107 {
108 public:
112  operator T() const { return _t; }
113 
116  T _t;
117 };
118 
122 template<class T>
124 {
125 public:
127  PythonQtNewOwnerOfThis(const T& t):_t(t) {}
129  operator T() const { return _t; }
130 
133  T _t;
134 };
135 
136 
138 template<class T1, class T2> int PythonQtUpcastingOffset() {
139  return ((reinterpret_cast<char*>(static_cast<T2*>(reinterpret_cast<T1*>(0x100))))
140  - (reinterpret_cast<char*>(reinterpret_cast<T1*>(0x100))));
141 }
142 
145 
147 template<class T> QObject* PythonQtCreateObject() { return new T(); }
148 
150 #ifdef PY3K
151 #define QStringToPythonConstCharPointer(arg) ((arg).toUtf8().constData())
152 #define QStringToPythonCharPointer(arg) ((arg).toUtf8().data())
153 #define QStringToPythonEncoding(arg) ((arg).toUtf8())
154 #else
155 #define QStringToPythonConstCharPointer(arg) ((arg).toLatin1().constData())
156 #define QStringToPythonCharPointer(arg) ((arg).toLatin1().data())
157 #define QStringToPythonEncoding(arg) ((arg).toLatin1())
158 #endif
159 
161 
169 class PYTHONQT_EXPORT PythonQt : public QObject {
170 
171  Q_OBJECT
172 
173 public:
174 
176  enum InitFlags {
177  RedirectStdOut = 1,
178  IgnoreSiteModule = 2,
179  ExternalHelp = 4,
180  PythonAlreadyInitialized = 8
181  };
182 
184  enum TypeSlots {
185  Type_Add = 1,
186  Type_Subtract = 1 << 1,
187  Type_Multiply = 1 << 2,
188  Type_Divide = 1 << 3,
189  Type_Mod = 1 << 4,
190  Type_And = 1 << 5,
191  Type_Or = 1 << 6,
192  Type_Xor = 1 << 7,
193  Type_LShift = 1 << 8,
194  Type_RShift = 1 << 9,
195 
196  Type_InplaceAdd = 1 << 10,
197  Type_InplaceSubtract = 1 << 11,
198  Type_InplaceMultiply = 1 << 12,
199  Type_InplaceDivide = 1 << 13,
200  Type_InplaceMod = 1 << 14,
201  Type_InplaceAnd = 1 << 15,
202  Type_InplaceOr = 1 << 16,
203  Type_InplaceXor = 1 << 17,
204  Type_InplaceLShift = 1 << 18,
205  Type_InplaceRShift = 1 << 19,
206 
207  Type_Length = 1 << 20,
208  Type_MappingSetItem = 1 << 21,
209  Type_MappingGetItem = 1 << 22,
210 
211  Type_EnterExit = 1 << 23,
212 
213  Type_Invert = 1 << 29,
214  Type_RichCompare = 1 << 30,
215  Type_NonZero = 1 << 31,
216 
217  };
218 
221  Enter = 1,
222  Leave = 2
223  };
224 
227  typedef void ProfilingCB(ProfilingCallbackState state, const char* className, const char* methodName, PyObject* args);
228 
229  //---------------------------------------------------------------------------
231 
232 
236  static void init(int flags = IgnoreSiteModule | RedirectStdOut, const QByteArray& pythonQtModuleName = QByteArray());
237 
239  static void cleanup();
240 
242  static PythonQt* self();
243 
245 
247  enum ObjectType {
253  CallOverloads
254  };
255 
256 
257  //---------------------------------------------------------------------------
259 
260 
263  void setRedirectStdInCallback(PythonQtInputChangedCB* callback, void * callbackData = nullptr);
264 
268 
270 
271  //---------------------------------------------------------------------------
273 
274 
277 
280  PythonQtObjectPtr importModule(const QString& name);
281 
286  PythonQtObjectPtr createModuleFromFile(const QString& name, const QString& filename);
287 
292  PythonQtObjectPtr createModuleFromScript(const QString& name, const QString& script = QString());
293 
297 
299 
300  //---------------------------------------------------------------------------
302 
303 
305  void overwriteSysPath(const QStringList& paths);
306 
308  void addSysPath(const QString& path);
309 
311  void setModuleImportPath(PyObject* module, const QStringList& paths);
312 
314 
315  //---------------------------------------------------------------------------
317 
318 
320  /* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
321  you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
322  void registerClass(const QMetaObject* metaobject, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr);
323 
326 
332  void registerCPPClass(const char* typeName, const char* parentTypeName = nullptr, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr);
333 
336  void registerQObjectClassNames(const QStringList& names);
337 
342  bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset=0);
343 
346 
348 
349  //---------------------------------------------------------------------------
351 
352 
354  PythonQtObjectPtr parseFile(const QString& filename);
355 
361 
364  QVariant evalCode(PyObject* object, PyObject* pycode);
365 
367  QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input);
368 
370  QVariant evalScript(const QString& script, PyObject* globals, PyObject* locals, int start);
371 
373  void evalFile(PyObject* object, const QString& filename);
374 
376 
377  //---------------------------------------------------------------------------
379 
380 
382  bool addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
383 
385  bool removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
386 
388  bool addSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
389 
391  bool removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
392 
395 
397 
398  //---------------------------------------------------------------------------
400 
401 
403  void addObject(PyObject* object, const QString& name, QObject* qObject);
404 
406  void addVariable(PyObject* object, const QString& name, const QVariant& v);
407 
409  void removeVariable(PyObject* module, const QString& name);
410 
412  QVariant getVariable(PyObject* object, const QString& name);
413 
415  QVariant getNativeVariable(PyObject* object, const QString& name);
416 
418  QStringList introspection(PyObject* object, const QString& objectname, ObjectType type);
420  QStringList introspectObject(PyObject* object, ObjectType type);
425  QStringList introspectType(const QString& typeName, ObjectType type);
426 
429  PythonQtObjectPtr lookupCallable(PyObject* object, const QString& name);
430 
432  QString getReturnTypeOfWrappedMethod(PyObject* module, const QString& objectname);
434  QString getReturnTypeOfWrappedMethod(const QString& typeName, const QString& methodName);
436 
437  //---------------------------------------------------------------------------
439 
440 
442  QVariant call(PyObject* object, const QString& callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
443 
445  QVariant call(PyObject* callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
446 
448  PyObject* callAndReturnPyObject(PyObject* callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
449 
451 
452  //---------------------------------------------------------------------------
454 
455 
460 
481  void addInstanceDecorators(QObject* o);
482 
485 
499  void addClassDecorators(QObject* o);
500 
502  void addDecorators(QObject* o);
503 
506 
509 
512 
515 
517 
518  //---------------------------------------------------------------------------
520 
521 
531  void setImporter(PythonQtImportFileInterface* importInterface);
532 
539  void installDefaultImporter() { setImporter(nullptr); }
540 
542  void setImporterIgnorePaths(const QStringList& paths);
543 
545  const QStringList& getImporterIgnorePaths();
546 
549 
551 
552  //---------------------------------------------------------------------------
554 
555 
557  static PythonQtPrivate* priv() { return _self->_p; }
558 
562 
565  bool handleError(bool printStack = true);
566 
568  bool hadError()const;
569 
572  void clearError();
573 
577 
581 
586 
588  static void qObjectNoLongerWrappedCB(QObject* o);
589 
592 
594  static QString qObjectMissingAttributeCallback(QObject* o, const QString& attribute);
595 
598 
601  PythonQtObjectPtr lookupObject(PyObject* module, const QString& name);
602 
604  void setProfilingCallback(ProfilingCB* cb);
605 
611  static void setEnableThreadSupport(bool flag);
612 
614 
615 Q_SIGNALS:
617  void pythonStdOut(const QString& str);
619  void pythonStdErr(const QString& str);
620 
622  void pythonHelpRequest(const QByteArray& cppClassName);
623 
627  void systemExitExceptionRaised(int exitCode);
628 
629 private:
630  void initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName);
631 
632  QString getReturnTypeOfWrappedMethodHelper(const PythonQtObjectPtr& variableObject, const QString& methodName, const QString& context);
633 
634  PyObject* getObjectByType(const QString& typeName);
635 
637  static void stdOutRedirectCB(const QString& str);
639  static void stdErrRedirectCB(const QString& str);
640 
642  PythonQtSignalReceiver* getSignalReceiver(QObject* obj);
643 
644  PythonQt(int flags, const QByteArray& pythonQtModuleName);
645  ~PythonQt() override;
646  static PythonQt* _self;
647  static int _uniqueModuleCount;
648 
649  PythonQtPrivate* _p;
650 
651 };
652 
653 class PythonQtDebugAPI;
654 class PythonQtConfigAPI;
655 
657 class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
658 
659  Q_OBJECT
660 
661 public:
663  ~PythonQtPrivate() override;
664 
666  StaticDecorator = 1,
667  ConstructorDecorator = 2,
668  DestructorDecorator = 4,
669  InstanceDecorator = 8,
670  AllDecorators = 0xffff
671  };
672 
675  void setTaskDoneCallback(const PythonQtObjectPtr& callable);
676 
679 
682 
684  const QStringList& sharedLibrarySuffixes() { return _sharedLibrarySuffixes; }
685 
687  bool isPythonQtObjectPtrMetaId(int id) { return _PythonQtObjectPtr_metaId == id; }
689  bool isPythonQtSafeObjectPtrMetaId(int id) { return _PythonQtSafeObjectPtr_metaId == id; }
691  bool isPythonQtAnyObjectPtrMetaId(int id) { return _PythonQtObjectPtr_metaId == id || _PythonQtSafeObjectPtr_metaId == id; }
692 
694  void addWrapperPointer(void* obj, PythonQtInstanceWrapper* wrapper);
696  void removeWrapperPointer(void* obj);
697 
699  void shellClassDeleted(void* shellClass);
700 
702  void* unwrapForeignWrapper(const QByteArray& classname, PyObject* obj);
703 
705  bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset);
706 
709 
712 
714  void removeSignalEmitter(QObject* obj);
715 
717  PyObject* wrapQObject(QObject* obj);
718 
722  PyObject* wrapPtr(void* ptr, const QByteArray& name, bool passOwnership = false);
723 
725  static PyObject* wrapMemoryAsBuffer(const void* data, Py_ssize_t size);
726 
728  static PyObject* wrapMemoryAsBuffer(void* data, Py_ssize_t size);
729 
731  /* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
732  you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
733  void registerClass(const QMetaObject* metaobject, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr, PyObject* module = nullptr, int typeSlots = 0);
734 
737 
743  void registerCPPClass(const char* typeName, const char* parentTypeName = nullptr, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr, PyObject* module = nullptr, int typeSlots = 0);
744 
746  void registerGlobalNamespace(const char* typeName, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, const QMetaObject& metaObject, PyObject* module = nullptr);
747 
750  void registerQObjectClassNames(const QStringList& names);
751 
753  void addDecorators(QObject* o, int decoTypes);
754 
756  PythonQtClassWrapper* createNewPythonQtClassWrapper(PythonQtClassInfo* info, PyObject* module, const QByteArray& pythonClassName);
757 
759  static PyObject* createEnumValueInstance(PyObject* enumType, unsigned int enumValue);
760 
762  static PyObject* createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject);
763 
765  PythonQtInstanceWrapper* createNewPythonQtInstanceWrapper(QObject* obj, PythonQtClassInfo* info, void* wrappedPtr = nullptr);
766 
768  PythonQtClassInfo* getClassInfo(const QMetaObject* meta);
769 
771  PythonQtClassInfo* getClassInfo(const QByteArray& className);
772 
775  void registerLazyClass(const QByteArray& name, const QByteArray& moduleToImport);
776 
778  PythonQtObjectPtr createModule(const QString& name, PyObject* pycode);
779 
782 
784  static PyObject* dummyTuple();
785 
787  void handleVirtualOverloadReturnError(const char* signature, const PythonQtMethodInfo* methodInfo, PyObject* result);
788 
790  PythonQtObjectPtr pythonQtModule() const { return _pythonQtModule; }
791 
793  PythonQt::ProfilingCB* profilingCB() const { return _profilingCB; }
794 
796  QString getSignature(PyObject* object);
797 
799  bool isMethodDescriptor(PyObject* object) const;
800 
802  const QMetaObject* getDynamicMetaObject(PythonQtInstanceWrapper* wrapper, const QMetaObject* prototypeMetaObject);
803 
805  const QMetaObject* setupDynamicMetaObjectChain(PythonQtClassWrapper* type, const QMetaObject* prototypeMetaObject);
806 
808  const QMetaObject* buildDynamicMetaObject(PythonQtClassWrapper* type, const QMetaObject* prototypeMetaObject);
809 
811  int handleMetaCall(QObject* object, PythonQtInstanceWrapper* wrapper, QMetaObject::Call call, int id, void** args);
812 
814  void callMethodInPython(QMetaMethod &method, PythonQtInstanceWrapper* wrapper, void** args);
815 
816 private:
818  void setupSharedLibrarySuffixes();
819 
821  void createPythonQtClassWrapper(PythonQtClassInfo* info, const char* package, PyObject* module = nullptr);
822 
824  PyObject* packageByName(const char* name);
825 
827  PythonQtInstanceWrapper* findWrapperAndRemoveUnused(void* obj);
828 
830  QHash<void* , PythonQtInstanceWrapper *> _wrappedObjects;
831 
833  QHash<QByteArray, PythonQtClassInfo *> _knownClassInfos;
834 
836  QHash<QByteArray, bool> _knownQObjectClassNames;
837 
839  QHash<QByteArray, QByteArray> _knownLazyClasses;
840 
842  QHash<QObject* , PythonQtSignalReceiver *> _signalReceivers;
843 
845  PythonQtObjectPtr _pythonQtModule;
846 
848  QByteArray _pythonQtModuleName;
849 
851  PythonQtImportFileInterface* _importInterface;
852 
854  PythonQtQFileImporter* _defaultImporter;
855 
856  PythonQtQObjectNoLongerWrappedCB* _noLongerWrappedCB;
857  PythonQtQObjectWrappedCB* _wrappedCB;
858  PythonQtQObjectMissingAttributeCB* _qObjectMissingAttribCB;
859 
860  QStringList _importIgnorePaths;
861  QStringList _sharedLibrarySuffixes;
862 
863  PythonQtObjectPtr _pySourceFileLoader;
864  PythonQtObjectPtr _pySourcelessFileLoader;
865  PythonQtObjectPtr _pyEnsureFuture;
866  PythonQtObjectPtr _pyFutureClass;
867 
868  PythonQtObjectPtr _pyTaskDoneCallback;
869 
871  QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
872 
873  QList<PythonQtForeignWrapperFactory*> _foreignWrapperFactories;
874 
875  QHash<QByteArray, PyObject*> _packages;
876 
877  PythonQtClassInfo* _currentClassInfoForClassWrapperCreation;
878 
879  PythonQt::ProfilingCB* _profilingCB;
880 
881  PythonQtDebugAPI* _debugAPI;
882  PythonQtConfigAPI* _configAPI;
883 
884  int _initFlags;
885  int _PythonQtObjectPtr_metaId;
886  int _PythonQtSafeObjectPtr_metaId;
887 
888  bool _hadError;
889  bool _systemExitExceptionHandlerEnabled;
890 
891  friend class PythonQt;
892 };
893 
894 #endif
struct _object PyObject
QString PythonQtInputChangedCB(void *callData)
declares the callback that is called from the write() function
Definition: PythonQtStdIn.h:54
#define PYTHONQT_EXPORT
int PythonQtUpcastingOffset()
returns the offset that needs to be added to upcast an object of type T1 to T2
Definition: PythonQt.h:138
QObject * PythonQtQObjectCreatorFunctionCB()
callback to create a QObject lazily
Definition: PythonQt.h:144
void PythonQtQObjectWrappedCB(QObject *object)
Definition: PythonQt.h:74
void PythonQtShellSetInstanceWrapperCB(void *object, PythonQtInstanceWrapper *wrapper)
Definition: PythonQt.h:79
QObject * PythonQtCreateObject()
helper template to create a derived QObject class
Definition: PythonQt.h:147
void PythonQtVoidPtrCB(void *object)
Definition: PythonQt.h:73
QString PythonQtQObjectMissingAttributeCB(QObject *object, const QString &attribute)
Definition: PythonQt.h:77
void PythonQtQObjectNoLongerWrappedCB(QObject *object)
Definition: PythonQt.h:75
void * PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name)
Definition: PythonQt.h:76
void PythonQtSetInstanceWrapperOnShell(void *object, PythonQtInstanceWrapper *wrapper)
Definition: PythonQt.h:81
a class that stores all required information about a Qt object (and an optional associated C++ class ...
Some methods to set properties of PythonQt from Python.
Factory interface for C++ classes that can be wrapped by QObject objects.
Some helper methods that allow testing of the ownership.
stores information about a specific signal/slot/method
PythonQtNewOwnerOfThis(const T &t)
Allow conversion from T to PythonQtNewOwnerOfThis<T>
Definition: PythonQt.h:127
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
PythonQtPassOwnershipToCPP(const T &t)
Allow conversion from T to PythonQtPassOwnershipToCPP<T>
Definition: PythonQt.h:93
PythonQtPassOwnershipToPython(const T &t)
Allow conversion from T to PythonQtPassOwnershipToPython<T>
Definition: PythonQt.h:110
internal PythonQt details
Definition: PythonQt.h:657
static PyObject * createNewPythonQtEnumWrapper(const char *enumName, PyObject *parentObject)
helper that creates a new int derived class that represents the enum of the given name (returns a new...
void handleVirtualOverloadReturnError(const char *signature, const PythonQtMethodInfo *methodInfo, PyObject *result)
called by virtual overloads when a python return value can not be converted to the required Qt type
const QMetaObject * getDynamicMetaObject(PythonQtInstanceWrapper *wrapper, const QMetaObject *prototypeMetaObject)
get the dynamic meta object for the given wrapper. It will contain the signals/slots that have been a...
void registerClass(const QMetaObject *metaobject, const char *package=nullptr, PythonQtQObjectCreatorFunctionCB *wrapperCreator=nullptr, PythonQtShellSetInstanceWrapperCB *shell=nullptr, PyObject *module=nullptr, int typeSlots=0)
registers a QObject derived class to PythonQt (this is implicitly called by addObject as well)
PythonQtClassInfo * getClassInfo(const QByteArray &className)
get the class info for a meta object (if available)
static PyObject * wrapMemoryAsBuffer(void *data, Py_ssize_t size)
create a read-write buffer object from the given memory
void registerQObjectClassNames(const QStringList &names)
PythonQtClassInfo * lookupClassInfoAndCreateIfNotPresent(const char *typeName)
lookup existing classinfo and return new if not yet present
void removeWrapperPointer(void *obj)
remove the wrapper ptr again
void setTaskDoneCallback(const PythonQtObjectPtr &callable)
int handleMetaCall(QObject *object, PythonQtInstanceWrapper *wrapper, QMetaObject::Call call, int id, void **args)
redirected from shell classes, tries to call the given meta call on the Python wrapper.
void addPolymorphicHandler(const char *typeName, PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
bool isMethodDescriptor(PyObject *object) const
returns true if the object is a method descriptor (same as inspect.ismethoddescriptor() in inspect....
void * unwrapForeignWrapper(const QByteArray &classname, PyObject *obj)
try to unwrap the given object to a C++ pointer using the foreign wrapper factories
void registerCPPClass(const char *typeName, const char *parentTypeName=nullptr, const char *package=nullptr, PythonQtQObjectCreatorFunctionCB *wrapperCreator=nullptr, PythonQtShellSetInstanceWrapperCB *shell=nullptr, PyObject *module=nullptr, int typeSlots=0)
PythonQtClassInfo * currentClassInfoForClassWrapperCreation()
get the current class info (for the next PythonQtClassWrapper that is created) and reset it to NULL a...
PythonQtInstanceWrapper * createNewPythonQtInstanceWrapper(QObject *obj, PythonQtClassInfo *info, void *wrappedPtr=nullptr)
helper method that creates a PythonQtInstanceWrapper object and registers it in the object map
PyObject * wrapPtr(void *ptr, const QByteArray &name, bool passOwnership=false)
bool addParentClass(const char *typeName, const char *parentTypeName, int upcastingOffset)
add parent class relation
const QMetaObject * buildDynamicMetaObject(PythonQtClassWrapper *type, const QMetaObject *prototypeMetaObject)
builds and returns the dynamic meta object for the given type, derived from prototypeMetaObject.
PythonQtObjectPtr pythonQtModule() const
get access to the PythonQt module
Definition: PythonQt.h:790
PythonQtClassWrapper * createNewPythonQtClassWrapper(PythonQtClassInfo *info, PyObject *module, const QByteArray &pythonClassName)
helper method that creates a PythonQtClassWrapper object (returns a new reference)
bool isPythonQtSafeObjectPtrMetaId(int id)
returns if the id is the id for PythonQtSafeObjectPtr
Definition: PythonQt.h:689
QString getSignature(PyObject *object)
determines the signature of the given callable object (similar as pydoc)
PythonQtObjectPtr checkAndRunCoroutine(const PythonQtObjectPtr &object)
Runs the given coroutine (via asyncio), returns a scheduled task if it object is a coroutine.
bool isPythonQtObjectPtrMetaId(int id)
returns if the id is the id for PythonQtObjectPtr
Definition: PythonQt.h:687
const QMetaObject * setupDynamicMetaObjectChain(PythonQtClassWrapper *type, const QMetaObject *prototypeMetaObject)
recursively creates the dynamic meta object chain down to the Qt class wrapper.
PythonQtObjectPtr createModule(const QString &name, PyObject *pycode)
creates the new module from the given pycode
void callMethodInPython(QMetaMethod &method, PythonQtInstanceWrapper *wrapper, void **args)
calls the given method on Python function with same name.
bool isPythonQtAnyObjectPtrMetaId(int id)
returns if the id is either PythonQtObjectPtr or PythonQtSafeObjectPtr
Definition: PythonQt.h:691
void registerLazyClass(const QByteArray &name, const QByteArray &moduleToImport)
PythonQt::ProfilingCB * profilingCB() const
returns the profiling callback, which may be NULL
Definition: PythonQt.h:793
void addWrapperPointer(void *obj, PythonQtInstanceWrapper *wrapper)
add the wrapper pointer (for reuse if the same obj appears while wrapper still exists)
PyObject * wrapQObject(QObject *obj)
wrap the given QObject into a Python object (or return existing wrapper!)
static PyObject * createEnumValueInstance(PyObject *enumType, unsigned int enumValue)
create a new instance of the given enum type with given value (returns a new reference)
PythonQtClassInfo * getClassInfo(const QMetaObject *meta)
get the class info for a meta object (if available)
void shellClassDeleted(void *shellClass)
called by destructor of shells to allow invalidation of the Python wrapper
const QStringList & sharedLibrarySuffixes()
get the suffixes that are used for shared libraries
Definition: PythonQt.h:684
static PyObject * dummyTuple()
the dummy tuple (which is empty and may be used to detected that a wrapper is called from internal wr...
static PyObject * wrapMemoryAsBuffer(const void *data, Py_ssize_t size)
create a read-only buffer object from the given memory
void registerGlobalNamespace(const char *typeName, const char *package, PythonQtQObjectCreatorFunctionCB *wrapperCreator, const QMetaObject &metaObject, PyObject *module=nullptr)
Same as above, but all enums of the created wrapper will also be added to the given package and to th...
PythonQtObjectPtr createAsyncioFuture()
Creates a new asyncio.Future object.
void removeSignalEmitter(QObject *obj)
called when a signal emitting QObject is destroyed to remove the signal handler from the hash map
void addDecorators(QObject *o, int decoTypes)
add a decorator object
~PythonQtPrivate() override
default importer implementation using QFile to load python code
receives all signals for one QObject
The main interface to the Python Qt binding, realized as a singleton.
Definition: PythonQt.h:169
QVariant call(PyObject *object, const QString &callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
call the given python callable in the scope of object, returns the result converted to a QVariant
PythonQtObjectPtr lookupCallable(PyObject *object, const QString &name)
static QString qObjectMissingAttributeCallback(QObject *o, const QString &attribute)
call the callback if it is set
void systemExitExceptionRaised(int exitCode)
void evalFile(PyObject *object, const QString &filename)
evaluates the given script code from file
void addInstanceDecorators(QObject *o)
bool removeSignalHandler(QObject *obj, const char *signal, PyObject *receiver)
remove a signal handler from the given signal of obj
PythonQtObjectPtr createModuleFromFile(const QString &name, const QString &filename)
void setQObjectMissingAttributeCallback(PythonQtQObjectMissingAttributeCB *cb)
set a callback that is called when a QObject does not have a specific attribute.
void setSystemExitExceptionHandlerEnabled(bool value)
void addClassDecorators(QObject *o)
PythonQtObjectPtr importModule(const QString &name)
InitFlags
flags that can be passed to PythonQt::init()
Definition: PythonQt.h:176
QString getReturnTypeOfWrappedMethod(const QString &typeName, const QString &methodName)
returns the return type of the method methodName of a wrapped c++ type referenced by typeName
static void init(int flags=IgnoreSiteModule|RedirectStdOut, const QByteArray &pythonQtModuleName=QByteArray())
ObjectType
defines the object types for introspection
Definition: PythonQt.h:247
@ Anything
Definition: PythonQt.h:252
@ Module
Definition: PythonQt.h:251
@ Variable
Definition: PythonQt.h:250
@ Function
Definition: PythonQt.h:249
ProfilingCallbackState
enum for profiling callback
Definition: PythonQt.h:220
static void cleanup()
cleanup of the singleton
static void setEnableThreadSupport(bool flag)
void registerCPPClass(const char *typeName, const char *parentTypeName=nullptr, const char *package=nullptr, PythonQtQObjectCreatorFunctionCB *wrapperCreator=nullptr, PythonQtShellSetInstanceWrapperCB *shell=nullptr)
void addSysPath(const QString &path)
prepend a path to sys.path to allow importing from it
TypeSlots
flags that tell PythonQt which operators to expect on the registered type
Definition: PythonQt.h:184
bool addParentClass(const char *typeName, const char *parentTypeName, int upcastingOffset=0)
bool hadError() const
return true if handleError() has been called and an error occurred.
void registerQObjectClassNames(const QStringList &names)
void addWrapperFactory(PythonQtCppWrapperFactory *factory)
add the given factory to PythonQt (ownership stays with caller)
PythonQtObjectPtr parseFile(const QString &filename)
parses the given file (using PythonQt's own import mechanism) and returns the python code object,...
void registerClass(const QMetaObject *metaobject, const char *package=nullptr, PythonQtQObjectCreatorFunctionCB *wrapperCreator=nullptr, PythonQtShellSetInstanceWrapperCB *shell=nullptr)
registers a QObject derived class to PythonQt (this is implicitly called by addObject as well)
void setRedirectStdInCallbackEnabled(bool enabled)
void removeVariable(PyObject *module, const QString &name)
remove the given variable
void removeWrapperFactory(PythonQtCppWrapperFactory *factory)
remove the wrapper factory
bool addSignalHandler(QObject *obj, const char *signal, PyObject *receiver)
add a signal handler to the given signal of obj and connect it to a callable receiver
QVariant getNativeVariable(PyObject *object, const QString &name)
get the variable with the name of the object as QVariant of type PythonQtObjectPtr,...
QVariant evalScript(const QString &script, PyObject *globals, PyObject *locals, int start)
evaluates the given script code in context of given globals and locals and returns the result value
const QStringList & getImporterIgnorePaths()
get paths that the importer should ignore
QVariant evalCode(PyObject *object, PyObject *pycode)
QVariant evalScript(PyObject *object, const QString &script, int start=Py_file_input)
evaluates the given script code and returns the result value
static void qObjectNoLongerWrappedCB(QObject *o)
call the callback if it is set
void setRedirectStdInCallback(PythonQtInputChangedCB *callback, void *callbackData=nullptr)
void setImporter(PythonQtImportFileInterface *importInterface)
PyObject * callAndReturnPyObject(PyObject *callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
call the given python object, returns the result as new PyObject
PythonQtObjectPtr createModuleFromScript(const QString &name, const QString &script=QString())
QStringList introspection(PyObject *object, const QString &objectname, ObjectType type)
read vars etc. in scope of an object, optional looking inside of an object objectname
PyObject * helpCalled(PythonQtClassInfo *info)
called by internal help methods
void addObject(PyObject *object, const QString &name, QObject *qObject)
add the given qObject to the python object as a variable with name (it can be removed via clearVariab...
void pythonStdErr(const QString &str)
emitted when python outputs something to stderr (and redirection is turned on)
void installDefaultImporter()
Definition: PythonQt.h:539
void addPolymorphicHandler(const char *typeName, PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
void clearError()
void setModuleImportPath(PyObject *module, const QStringList &paths)
sets the path list of a module to the given list (important for local imports)
void removeSignalHandlers()
globally removes all signal handlers (connections between QObjects and Python).
void addVariable(PyObject *object, const QString &name, const QVariant &v)
add the given variable to the object
PythonQtObjectPtr getMainModule()
get the main module of python
static PythonQtPrivate * priv()
get access to internal data (should not be used on the public API, but is used by some C functions)
Definition: PythonQt.h:557
void addDecorators(QObject *o)
this will add the object both as class and instance decorator (ownership is passed to PythonQt)
void setQObjectWrappedCallback(PythonQtQObjectWrappedCB *cb)
set a callback that is called when a QObject with parent == NULL is wrapped by PythonQt
QVariant call(PyObject *callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
call the given python object, returns the result converted to a QVariant
bool systemExitExceptionHandlerEnabled() const
void addWrapperFactory(PythonQtForeignWrapperFactory *factory)
add the given factory to PythonQt (ownership stays with caller)
QVariant getVariable(PyObject *object, const QString &name)
get the variable with the name of the object, returns an invalid QVariant on error
void removeWrapperFactory(PythonQtForeignWrapperFactory *factory)
remove the wrapper factory
PythonQtObjectPtr lookupObject(PyObject *module, const QString &name)
void setImporterIgnorePaths(const QStringList &paths)
set paths that the importer should ignore
void pythonHelpRequest(const QByteArray &cppClassName)
emitted when help() is called on a PythonQt object and ExternalHelp is enabled
bool removeSignalHandler(QObject *obj, const char *signal, PyObject *module, const QString &objectname)
remove a signal handler from the given signal of obj
QString getReturnTypeOfWrappedMethod(PyObject *module, const QString &objectname)
returns the return type of the method of a wrapped c++ object referenced by objectname
bool addSignalHandler(QObject *obj, const char *signal, PyObject *module, const QString &objectname)
add a signal handler to the given signal of obj and connect it to a callable objectname in module
void setQObjectNoLongerWrappedCallback(PythonQtQObjectNoLongerWrappedCB *cb)
set a callback that is called when a QObject with parent == NULL is no longer wrapped by PythonQt
static PythonQtImportFileInterface * importInterface()
get access to the file importer (if set)
PythonQtObjectPtr parseFileWithPythonLoaders(const QString &filename)
void pythonStdOut(const QString &str)
emitted when python outputs something to stdout (and redirection is turned on)
void overwriteSysPath(const QStringList &paths)
overwrite the python sys path (call this directly after PythonQt::init() if you want to change the st...
void ProfilingCB(ProfilingCallbackState state, const char *className, const char *methodName, PyObject *args)
Definition: PythonQt.h:227
QStringList introspectObject(PyObject *object, ObjectType type)
read vars etc. in scope of the given object
void clearNotFoundCachedMembers()
bool handleError(bool printStack=true)
PythonQtObjectPtr createUniqueModule()
QStringList introspectType(const QString &typeName, ObjectType type)
void setProfilingCallback(ProfilingCB *cb)
sets a callback that is called before and after function calls for profiling
QByteArray methodName(const QMetaMethod &method)
Definition: PythonQtUtils.h:61
QByteArray typeName(const QMetaMethod &method)
Definition: PythonQtUtils.h:72
QByteArray signature(const QMetaMethod &method)
Definition: PythonQtUtils.h:53
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...