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
46#include "PythonQtUtils.h"
47#include "PythonQtSystem.h"
50#include "PythonQtSlot.h"
51#include "PythonQtObjectPtr.h"
52#include "PythonQtStdIn.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
64class PythonQtPrivate;
71
72typedef void PythonQtVoidPtrCB(void* object);
73typedef void PythonQtQObjectWrappedCB(QObject* object);
74typedef void PythonQtQObjectNoLongerWrappedCB(QObject* object);
75typedef void* PythonQtPolymorphicHandlerCB(const void* ptr, const char** class_name);
76typedef QString PythonQtQObjectMissingAttributeCB(QObject* object, const QString& attribute);
77
79
80template<class T>
82{
83 (reinterpret_cast<T*>(object))->_wrapper = wrapper;
84}
85
89template<class T>
91{
92public:
95 : _t(t)
96 {
97 }
99 operator T() const { return _t; }
100
103 T _t;
104};
105
109template<class T>
111{
112public:
115 : _t(t)
116 {
117 }
119 operator T() const { return _t; }
120
123 T _t;
124};
125
129template<class T>
131{
132public:
135 : _t(t)
136 {
137 }
139 operator T() const { return _t; }
140
143 T _t;
144};
145
147template<class T1, class T2>
149{
150 return ((reinterpret_cast<char*>(static_cast<T2*>(reinterpret_cast<T1*>(0x100))))
151 - (reinterpret_cast<char*>(reinterpret_cast<T1*>(0x100))));
152}
153
156
158template<class T>
160{
161 return new T();
162}
163
165#define QStringToPythonConstCharPointer(arg) ((arg).toUtf8().constData())
166#define QStringToPythonCharPointer(arg) ((arg).toUtf8().data())
167#define QStringToPythonEncoding(arg) ((arg).toUtf8())
168
170
178class PYTHONQT_EXPORT PythonQt : public QObject
179{
180
181 Q_OBJECT
182
183public:
184
187 RedirectStdOut = 1,
188 IgnoreSiteModule = 2,
189 ExternalHelp = 4,
190 PythonAlreadyInitialized = 8
191 };
192
195 Type_Add = 1,
196 Type_Subtract = 1 << 1,
197 Type_Multiply = 1 << 2,
198 Type_Divide = 1 << 3,
199 Type_Mod = 1 << 4,
200 Type_And = 1 << 5,
201 Type_Or = 1 << 6,
202 Type_Xor = 1 << 7,
203 Type_LShift = 1 << 8,
204 Type_RShift = 1 << 9,
205
206 Type_InplaceAdd = 1 << 10,
207 Type_InplaceSubtract = 1 << 11,
208 Type_InplaceMultiply = 1 << 12,
209 Type_InplaceDivide = 1 << 13,
210 Type_InplaceMod = 1 << 14,
211 Type_InplaceAnd = 1 << 15,
212 Type_InplaceOr = 1 << 16,
213 Type_InplaceXor = 1 << 17,
214 Type_InplaceLShift = 1 << 18,
215 Type_InplaceRShift = 1 << 19,
216
217 Type_Length = 1 << 20,
218 Type_MappingSetItem = 1 << 21,
219 Type_MappingGetItem = 1 << 22,
220
221 Type_EnterExit = 1 << 23,
222
223 Type_Invert = 1 << 29,
224 Type_RichCompare = 1 << 30,
225 Type_NonZero = 1 << 31,
226
227 };
228
230 enum ProfilingCallbackState { Enter = 1, Leave = 2 };
231
234 typedef void ProfilingCB(ProfilingCallbackState state, const char* className, const char* methodName, PyObject* args);
235
236 //---------------------------------------------------------------------------
238
239
243 static void init(int flags = IgnoreSiteModule | RedirectStdOut, const QByteArray& pythonQtModuleName = QByteArray());
244
246 static void cleanup();
247
249 static PythonQt* self();
250
252
254 enum ObjectType { Class, Function, Variable, Module, Anything, CallOverloads };
255
256 //---------------------------------------------------------------------------
258
259
262 void setRedirectStdInCallback(PythonQtInputChangedCB* callback, void* callbackData = nullptr);
263
267
269
270 //---------------------------------------------------------------------------
272
273
276
279
281
282 //---------------------------------------------------------------------------
284
285
288
291 PythonQtObjectPtr importModule(const QString& name);
292
297 PythonQtObjectPtr createModuleFromFile(const QString& name, const QString& filename);
298
303 PythonQtObjectPtr createModuleFromScript(const QString& name, const QString& script = QString());
304
308
310
311 //---------------------------------------------------------------------------
313
314
316 void overwriteSysPath(const QStringList& paths);
317
319 void addSysPath(const QString& path);
320
322 void setModuleImportPath(PyObject* module, const QStringList& paths);
323
325
326 //---------------------------------------------------------------------------
328
329
331 /* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
332 you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
333 void registerClass(const QMetaObject* metaobject, const char* package = nullptr,
334 PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr);
335
338
344 void registerCPPClass(const char* typeName, const char* parentTypeName = nullptr, const char* package = nullptr,
345 PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr);
346
349 void registerQObjectClassNames(const QStringList& names);
350
355 bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset = 0);
356
359
361
362 //---------------------------------------------------------------------------
364
365
367 PythonQtObjectPtr parseFile(const QString& filename);
368
373
376 QVariant evalCode(PyObject* object, PyObject* pycode);
377
379 QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input);
380
382 QVariant evalScript(const QString& script, PyObject* globals, PyObject* locals, int start);
383
385 void evalFile(PyObject* object, const QString& filename);
386
388
389 //---------------------------------------------------------------------------
391
392
394 bool addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
395
397 bool removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
398
400 bool addSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
401
403 bool removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
404
407
409
410 //---------------------------------------------------------------------------
412
413
415 void addObject(PyObject* object, const QString& name, QObject* qObject);
416
418 void addVariable(PyObject* object, const QString& name, const QVariant& v);
419
421 void removeVariable(PyObject* module, const QString& name);
422
424 QVariant getVariable(PyObject* object, const QString& name);
425
427 QVariant getNativeVariable(PyObject* object, const QString& name);
428
430 QStringList introspection(PyObject* object, const QString& objectname, ObjectType type);
432 QStringList introspectObject(PyObject* object, ObjectType type);
437 QStringList introspectType(const QString& typeName, ObjectType type);
438
441 PythonQtObjectPtr lookupCallable(PyObject* object, const QString& name);
442
444 QString getReturnTypeOfWrappedMethod(PyObject* module, const QString& objectname);
446 QString getReturnTypeOfWrappedMethod(const QString& typeName, const QString& methodName);
448
449 //---------------------------------------------------------------------------
451
452
454 QVariant call(PyObject* object, const QString& callable, const QVariantList& args = QVariantList(),
455 const QVariantMap& kwargs = QVariantMap());
456
458 QVariant call(PyObject* callable, const QVariantList& args = QVariantList(),
459 const QVariantMap& kwargs = QVariantMap());
460
462 PyObject* callAndReturnPyObject(PyObject* callable, const QVariantList& args = QVariantList(),
463 const QVariantMap& kwargs = QVariantMap());
464
466
467 //---------------------------------------------------------------------------
469
470
475
496 void addInstanceDecorators(QObject* o);
497
500
514 void addClassDecorators(QObject* o);
515
517 void addDecorators(QObject* o);
518
521
524
527
530
532
533 //---------------------------------------------------------------------------
535
536
547
554 void installDefaultImporter() { setImporter(nullptr); }
555
557 void setImporterIgnorePaths(const QStringList& paths);
558
560 const QStringList& getImporterIgnorePaths();
561
564
566
567 //---------------------------------------------------------------------------
569
570
572 static PythonQtPrivate* priv() { return _self ? _self->_p : nullptr; }
573
577
580 bool handleError(bool printStack = true);
581
583 bool hadError() const;
584
588
592
596
601
603 static void qObjectNoLongerWrappedCB(QObject* o);
604
607
609 static QString qObjectMissingAttributeCallback(QObject* o, const QString& attribute);
610
613
616 PythonQtObjectPtr lookupObject(PyObject* module, const QString& name);
617
619 void setProfilingCallback(ProfilingCB* cb);
620
626 static void setEnableThreadSupport(bool flag);
627
629
630Q_SIGNALS:
632 void pythonStdOut(const QString& str);
634 void pythonStdErr(const QString& str);
635
637 void pythonHelpRequest(const QByteArray& cppClassName);
638
642 void systemExitExceptionRaised(int exitCode);
643
644private:
645 void initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName);
646
647 QString getReturnTypeOfWrappedMethodHelper(const PythonQtObjectPtr& variableObject, const QString& methodName,
648 const QString& context);
649
650 PyObject* getObjectByType(const QString& typeName);
651
653 static void stdOutRedirectCB(const QString& str);
655 static void stdErrRedirectCB(const QString& str);
656
658 PythonQtSignalReceiver* getSignalReceiver(QObject* obj);
659
660 PythonQt(int flags, const QByteArray& pythonQtModuleName);
661 ~PythonQt() override;
662 static PythonQt* _self;
663 static int _uniqueModuleCount;
664
665 PythonQtPrivate* _p;
666};
667
668class PythonQtDebugAPI;
670
672class PYTHONQT_EXPORT PythonQtPrivate : public QObject
673{
674
675 Q_OBJECT
676
677public:
680
682 StaticDecorator = 1,
683 ConstructorDecorator = 2,
684 DestructorDecorator = 4,
685 InstanceDecorator = 8,
686 AllDecorators = 0xffff
687 };
688
692
695
698
700 const QStringList& sharedLibrarySuffixes() { return _sharedLibrarySuffixes; }
701
703 bool isPythonQtObjectPtrMetaId(int id) { return _PythonQtObjectPtr_metaId == id; }
705 bool isPythonQtSafeObjectPtrMetaId(int id) { return _PythonQtSafeObjectPtr_metaId == id; }
708 {
709 return _PythonQtObjectPtr_metaId == id || _PythonQtSafeObjectPtr_metaId == id;
710 }
711
715 void removeWrapperPointer(void* obj);
716
718 void shellClassDeleted(void* shellClass);
719
721 void* unwrapForeignWrapper(const QByteArray& classname, PyObject* obj);
722
724 bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset);
725
728
731
733 void removeSignalEmitter(QObject* obj);
734
736 PyObject* wrapQObject(QObject* obj);
737
741 PyObject* wrapPtr(void* ptr, const QByteArray& name, bool passOwnership = false);
742
744 static PyObject* wrapMemoryAsBuffer(const void* data, Py_ssize_t size);
745
747 static PyObject* wrapMemoryAsBuffer(void* data, Py_ssize_t size);
748
750 /* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
751 you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
752 void registerClass(const QMetaObject* metaobject, const char* package = nullptr,
753 PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr,
754 PyObject* module = nullptr, int typeSlots = 0);
755
758
764 void registerCPPClass(const char* typeName, const char* parentTypeName = nullptr, const char* package = nullptr,
765 PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr,
766 PyObject* module = nullptr, int typeSlots = 0);
767
769 void registerGlobalNamespace(const char* typeName, const char* package,
770 PythonQtQObjectCreatorFunctionCB* wrapperCreator, const QMetaObject& metaObject, PyObject* module = nullptr);
771
774 void registerQObjectClassNames(const QStringList& names);
775
777 void addDecorators(QObject* o, int decoTypes);
778
781 const QByteArray& pythonClassName);
782
784 static PyObject* createEnumValueInstance(PyObject* enumType, unsigned int enumValue);
785
787 static PyObject* createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject);
788
791 void* wrappedPtr = nullptr);
792
794 PythonQtClassInfo* getClassInfo(const QMetaObject* meta);
795
797 PythonQtClassInfo* getClassInfo(const QByteArray& className);
798
801 void registerLazyClass(const QByteArray& name, const QByteArray& moduleToImport);
802
804 PythonQtObjectPtr createModule(const QString& name, PyObject* pycode);
805
808
811
813 void handleVirtualOverloadReturnError(const char* signature, const PythonQtMethodInfo* methodInfo, PyObject* result);
814
816 PythonQtObjectPtr pythonQtModule() const { return _pythonQtModule; }
817
819 PythonQt::ProfilingCB* profilingCB() const { return _profilingCB; }
820
822 QString getSignature(PyObject* object);
823
825 bool isMethodDescriptor(PyObject* object) const;
826
828 const QMetaObject* getDynamicMetaObject(PythonQtInstanceWrapper* wrapper, const QMetaObject* prototypeMetaObject);
829
831 const QMetaObject* setupDynamicMetaObjectChain(PythonQtClassWrapper* type, const QMetaObject* prototypeMetaObject);
832
834 const QMetaObject* buildDynamicMetaObject(PythonQtClassWrapper* type, const QMetaObject* prototypeMetaObject);
835
837 int handleMetaCall(QObject* object, PythonQtInstanceWrapper* wrapper, QMetaObject::Call call, int id, void** args);
838
840 void callMethodInPython(QMetaMethod& method, PythonQtInstanceWrapper* wrapper, void** args);
841
842private:
844 void setupSharedLibrarySuffixes();
845
847 void createPythonQtClassWrapper(PythonQtClassInfo* info, const char* package, PyObject* module = nullptr);
848
850 PyObject* packageByName(const char* name);
851
853 PythonQtInstanceWrapper* findWrapperAndRemoveUnused(void* obj);
854
856 QHash<void*, PythonQtInstanceWrapper*> _wrappedObjects;
857
859 QHash<QByteArray, PythonQtClassInfo*> _knownClassInfos;
860
862 QHash<QByteArray, bool> _knownQObjectClassNames;
863
865 QHash<QByteArray, QByteArray> _knownLazyClasses;
866
868 QHash<QObject*, PythonQtSignalReceiver*> _signalReceivers;
869
871 PythonQtObjectPtr _pythonQtModule;
872
874 QByteArray _pythonQtModuleName;
875
877 PythonQtImportFileInterface* _importInterface;
878
880 PythonQtQFileImporter* _defaultImporter;
881
882 PythonQtQObjectNoLongerWrappedCB* _noLongerWrappedCB;
883 PythonQtQObjectWrappedCB* _wrappedCB;
884 PythonQtQObjectMissingAttributeCB* _qObjectMissingAttribCB;
885
886 QStringList _importIgnorePaths;
887 QStringList _sharedLibrarySuffixes;
888
889 PythonQtObjectPtr _pySourceFileLoader;
890 PythonQtObjectPtr _pySourcelessFileLoader;
891 PythonQtObjectPtr _pyEnsureFuture;
892 PythonQtObjectPtr _pyFutureClass;
893
894 PythonQtObjectPtr _pyTaskDoneCallback;
895
897 QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
898
899 QList<PythonQtForeignWrapperFactory*> _foreignWrapperFactories;
900
901 QHash<QByteArray, PyObject*> _packages;
902
903 PythonQtClassInfo* _currentClassInfoForClassWrapperCreation;
904
905 PythonQt::ProfilingCB* _profilingCB;
906
907 PythonQtDebugAPI* _debugAPI;
908 PythonQtConfigAPI* _configAPI;
909
910 int _initFlags;
911 int _PythonQtObjectPtr_metaId;
912 int _PythonQtSafeObjectPtr_metaId;
913
914 bool _hadError;
915 bool _systemExitExceptionHandlerEnabled;
916
917 friend class PythonQt;
918};
919
920#endif
struct _object PyObject
QString PythonQtInputChangedCB(void *callData)
declares the callback that is called from the write() function
#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:148
QObject * PythonQtCreateObject()
helper template to create a derived QObject class
Definition PythonQt.h:159
void PythonQtQObjectWrappedCB(QObject *object)
Definition PythonQt.h:73
void PythonQtShellSetInstanceWrapperCB(void *object, PythonQtInstanceWrapper *wrapper)
Definition PythonQt.h:78
void * PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name)
Definition PythonQt.h:75
void PythonQtVoidPtrCB(void *object)
Definition PythonQt.h:72
QString PythonQtQObjectMissingAttributeCB(QObject *object, const QString &attribute)
Definition PythonQt.h:76
void PythonQtQObjectNoLongerWrappedCB(QObject *object)
Definition PythonQt.h:74
void PythonQtSetInstanceWrapperOnShell(void *object, PythonQtInstanceWrapper *wrapper)
Definition PythonQt.h:81
QObject * PythonQtQObjectCreatorFunctionCB()
callback to create a QObject lazily
Definition PythonQt.h:155
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:134
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:94
PythonQtPassOwnershipToPython(const T &t)
Allow conversion from T to PythonQtPassOwnershipToPython<T>
Definition PythonQt.h:114
internal PythonQt details
Definition PythonQt.h:673
void * unwrapForeignWrapper(const QByteArray &classname, PyObject *obj)
try to unwrap the given object to a C++ pointer using the foreign wrapper factories
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
static PyObject * wrapMemoryAsBuffer(const void *data, Py_ssize_t size)
create a read-only buffer object from the given memory
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)
const QMetaObject * setupDynamicMetaObjectChain(PythonQtClassWrapper *type, const QMetaObject *prototypeMetaObject)
recursively creates the dynamic meta object chain down to the Qt class wrapper.
PythonQtClassWrapper * createNewPythonQtClassWrapper(PythonQtClassInfo *info, PyObject *module, const QByteArray &pythonClassName)
helper method that creates a PythonQtClassWrapper object (returns a new reference)
void registerQObjectClassNames(const QStringList &names)
const QStringList & sharedLibrarySuffixes()
get the suffixes that are used for shared libraries
Definition PythonQt.h:700
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.
PyObject * wrapQObject(QObject *obj)
wrap the given QObject into a Python object (or return existing wrapper!)
void addPolymorphicHandler(const char *typeName, PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
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...
bool isMethodDescriptor(PyObject *object) const
returns true if the object is a method descriptor (same as inspect.ismethoddescriptor() in inspect....
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)
static PyObject * wrapMemoryAsBuffer(void *data, Py_ssize_t size)
create a read-write buffer object from the given memory
bool addParentClass(const char *typeName, const char *parentTypeName, int upcastingOffset)
add parent class relation
PythonQtObjectPtr pythonQtModule() const
get access to the PythonQt module
Definition PythonQt.h:816
bool isPythonQtSafeObjectPtrMetaId(int id)
returns if the id is the id for PythonQtSafeObjectPtr
Definition PythonQt.h:705
PythonQtClassInfo * currentClassInfoForClassWrapperCreation()
get the current class info (for the next PythonQtClassWrapper that is created) and reset it to NULL a...
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:703
PythonQtClassInfo * lookupClassInfoAndCreateIfNotPresent(const char *typeName)
lookup existing classinfo and return new if not yet present
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:707
const QMetaObject * buildDynamicMetaObject(PythonQtClassWrapper *type, const QMetaObject *prototypeMetaObject)
builds and returns the dynamic meta object for the given type, derived from prototypeMetaObject.
void registerLazyClass(const QByteArray &name, const QByteArray &moduleToImport)
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...
static PyObject * createEnumValueInstance(PyObject *enumType, unsigned int enumValue)
create a new instance of the given enum type with given value (returns a new reference)
static PyObject * dummyTuple()
the dummy tuple (which is empty and may be used to detected that a wrapper is called from internal wr...
PyObject * wrapPtr(void *ptr, const QByteArray &name, bool passOwnership=false)
void addWrapperPointer(void *obj, PythonQtInstanceWrapper *wrapper)
add the wrapper pointer (for reuse if the same obj appears while wrapper still exists)
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
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
PythonQtClassInfo * getClassInfo(const QByteArray &className)
get the class info for a meta object (if available)
PythonQtInstanceWrapper * createNewPythonQtInstanceWrapper(QObject *obj, PythonQtClassInfo *info, void *wrappedPtr=nullptr)
helper method that creates a PythonQtInstanceWrapper object and registers it in the object map
PythonQt::ProfilingCB * profilingCB() const
returns the profiling callback, which may be NULL
Definition PythonQt.h:819
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:179
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:186
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:254
ProfilingCallbackState
enum for profiling callback
Definition PythonQt.h:230
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:194
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
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)
const QStringList & getImporterIgnorePaths()
get paths that the importer should ignore
void setImporter(PythonQtImportFileInterface *importInterface)
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
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:554
void addPolymorphicHandler(const char *typeName, PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
PyObject * helpCalled(PythonQtClassInfo *info)
called by internal help methods
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
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
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:572
PythonQtObjectPtr lookupObject(PyObject *module, const QString &name)
void setImporterIgnorePaths(const QStringList &paths)
set paths that the importer should ignore
void setRedirectStdOutCallbackEnabled(bool enabled)
enable or disable std out/err redirection to pythonStdOut() and pythonStdErr() signals.
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
PyObject * callAndReturnPyObject(PyObject *callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
call the given python object, returns the result as new PyObject
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
bool redirectStdOutCallbackEnabled() const
return true if std out/err redirection is enabled.
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:234
static PythonQt * self()
get the singleton instance
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
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...