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
63
65class PythonQtPrivate;
72
73typedef void PythonQtVoidPtrCB(void* object);
74typedef void PythonQtQObjectWrappedCB(QObject* object);
75typedef void PythonQtQObjectNoLongerWrappedCB(QObject* object);
76typedef void* PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name);
77typedef QString PythonQtQObjectMissingAttributeCB(QObject* object, const QString& attribute);
78
80
81template<class T> void PythonQtSetInstanceWrapperOnShell(void* object, PythonQtInstanceWrapper* wrapper) {
82 (reinterpret_cast<T*>(object))->_wrapper = wrapper;
83}
84
88template<class T>
90{
91 public:
95 operator T() const { return _t; }
96
99 T _t;
100};
101
105template<class T>
107{
108public:
112 operator T() const { return _t; }
113
116 T _t;
117};
118
122template<class T>
124{
125public:
127 PythonQtNewOwnerOfThis(const T& t):_t(t) {}
129 operator T() const { return _t; }
130
133 T _t;
134};
135
136
138template<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
147template<class T> QObject* PythonQtCreateObject() { return new T(); }
148
150#define QStringToPythonConstCharPointer(arg) ((arg).toUtf8().constData())
151#define QStringToPythonCharPointer(arg) ((arg).toUtf8().data())
152#define QStringToPythonEncoding(arg) ((arg).toUtf8())
153
155
163class PYTHONQT_EXPORT PythonQt : public QObject {
164
165 Q_OBJECT
166
167public:
168
171 RedirectStdOut = 1,
172 IgnoreSiteModule = 2,
173 ExternalHelp = 4,
174 PythonAlreadyInitialized = 8
175 };
176
179 Type_Add = 1,
180 Type_Subtract = 1 << 1,
181 Type_Multiply = 1 << 2,
182 Type_Divide = 1 << 3,
183 Type_Mod = 1 << 4,
184 Type_And = 1 << 5,
185 Type_Or = 1 << 6,
186 Type_Xor = 1 << 7,
187 Type_LShift = 1 << 8,
188 Type_RShift = 1 << 9,
189
190 Type_InplaceAdd = 1 << 10,
191 Type_InplaceSubtract = 1 << 11,
192 Type_InplaceMultiply = 1 << 12,
193 Type_InplaceDivide = 1 << 13,
194 Type_InplaceMod = 1 << 14,
195 Type_InplaceAnd = 1 << 15,
196 Type_InplaceOr = 1 << 16,
197 Type_InplaceXor = 1 << 17,
198 Type_InplaceLShift = 1 << 18,
199 Type_InplaceRShift = 1 << 19,
200
201 Type_Length = 1 << 20,
202 Type_MappingSetItem = 1 << 21,
203 Type_MappingGetItem = 1 << 22,
204
205 Type_EnterExit = 1 << 23,
206
207 Type_Invert = 1 << 29,
208 Type_RichCompare = 1 << 30,
209 Type_NonZero = 1 << 31,
210
211 };
212
215 Enter = 1,
216 Leave = 2
217 };
218
221 typedef void ProfilingCB(ProfilingCallbackState state, const char* className, const char* methodName, PyObject* args);
222
223 //---------------------------------------------------------------------------
225
226
230 static void init(int flags = IgnoreSiteModule | RedirectStdOut, const QByteArray& pythonQtModuleName = QByteArray());
231
233 static void cleanup();
234
236 static PythonQt* self();
237
239
249
250
251 //---------------------------------------------------------------------------
253
254
257 void setRedirectStdInCallback(PythonQtInputChangedCB* callback, void * callbackData = nullptr);
258
262
264
265 //---------------------------------------------------------------------------
267
268
271
274
276
277 //---------------------------------------------------------------------------
279
280
283
286 PythonQtObjectPtr importModule(const QString& name);
287
292 PythonQtObjectPtr createModuleFromFile(const QString& name, const QString& filename);
293
298 PythonQtObjectPtr createModuleFromScript(const QString& name, const QString& script = QString());
299
303
305
306 //---------------------------------------------------------------------------
308
309
311 void overwriteSysPath(const QStringList& paths);
312
314 void addSysPath(const QString& path);
315
317 void setModuleImportPath(PyObject* module, const QStringList& paths);
318
320
321 //---------------------------------------------------------------------------
323
324
326 /* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
327 you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
328 void registerClass(const QMetaObject* metaobject, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr);
329
332
338 void registerCPPClass(const char* typeName, const char* parentTypeName = nullptr, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr);
339
342 void registerQObjectClassNames(const QStringList& names);
343
348 bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset=0);
349
352
354
355 //---------------------------------------------------------------------------
357
358
360 PythonQtObjectPtr parseFile(const QString& filename);
361
366
369 QVariant evalCode(PyObject* object, PyObject* pycode);
370
372 QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input);
373
375 QVariant evalScript(const QString& script, PyObject* globals, PyObject* locals, int start);
376
378 void evalFile(PyObject* object, const QString& filename);
379
381
382 //---------------------------------------------------------------------------
384
385
387 bool addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
388
390 bool removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
391
393 bool addSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
394
396 bool removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
397
400
402
403 //---------------------------------------------------------------------------
405
406
408 void addObject(PyObject* object, const QString& name, QObject* qObject);
409
411 void addVariable(PyObject* object, const QString& name, const QVariant& v);
412
414 void removeVariable(PyObject* module, const QString& name);
415
417 QVariant getVariable(PyObject* object, const QString& name);
418
420 QVariant getNativeVariable(PyObject* object, const QString& name);
421
423 QStringList introspection(PyObject* object, const QString& objectname, ObjectType type);
425 QStringList introspectObject(PyObject* object, ObjectType type);
430 QStringList introspectType(const QString& typeName, ObjectType type);
431
434 PythonQtObjectPtr lookupCallable(PyObject* object, const QString& name);
435
437 QString getReturnTypeOfWrappedMethod(PyObject* module, const QString& objectname);
439 QString getReturnTypeOfWrappedMethod(const QString& typeName, const QString& methodName);
441
442 //---------------------------------------------------------------------------
444
445
447 QVariant call(PyObject* object, const QString& callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
448
450 QVariant call(PyObject* callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
451
453 PyObject* callAndReturnPyObject(PyObject* callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
454
456
457 //---------------------------------------------------------------------------
459
460
465
486 void addInstanceDecorators(QObject* o);
487
490
504 void addClassDecorators(QObject* o);
505
507 void addDecorators(QObject* o);
508
511
514
517
520
522
523 //---------------------------------------------------------------------------
525
526
537
544 void installDefaultImporter() { setImporter(nullptr); }
545
547 void setImporterIgnorePaths(const QStringList& paths);
548
550 const QStringList& getImporterIgnorePaths();
551
554
556
557 //---------------------------------------------------------------------------
559
560
562 static PythonQtPrivate* priv() { return _self ? _self->_p : nullptr; }
563
567
570 bool handleError(bool printStack = true);
571
573 bool hadError()const;
574
578
582
586
591
593 static void qObjectNoLongerWrappedCB(QObject* o);
594
597
599 static QString qObjectMissingAttributeCallback(QObject* o, const QString& attribute);
600
603
606 PythonQtObjectPtr lookupObject(PyObject* module, const QString& name);
607
609 void setProfilingCallback(ProfilingCB* cb);
610
616 static void setEnableThreadSupport(bool flag);
617
619
620Q_SIGNALS:
622 void pythonStdOut(const QString& str);
624 void pythonStdErr(const QString& str);
625
627 void pythonHelpRequest(const QByteArray& cppClassName);
628
632 void systemExitExceptionRaised(int exitCode);
633
634private:
635 void initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName);
636
637 QString getReturnTypeOfWrappedMethodHelper(const PythonQtObjectPtr& variableObject, const QString& methodName, const QString& context);
638
639 PyObject* getObjectByType(const QString& typeName);
640
642 static void stdOutRedirectCB(const QString& str);
644 static void stdErrRedirectCB(const QString& str);
645
647 PythonQtSignalReceiver* getSignalReceiver(QObject* obj);
648
649 PythonQt(int flags, const QByteArray& pythonQtModuleName);
650 ~PythonQt() override;
651 static PythonQt* _self;
652 static int _uniqueModuleCount;
653
654 PythonQtPrivate* _p;
655
656};
657
658class PythonQtDebugAPI;
660
662class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
663
664 Q_OBJECT
665
666public:
669
671 StaticDecorator = 1,
672 ConstructorDecorator = 2,
673 DestructorDecorator = 4,
674 InstanceDecorator = 8,
675 AllDecorators = 0xffff
676 };
677
681
684
687
689 const QStringList& sharedLibrarySuffixes() { return _sharedLibrarySuffixes; }
690
692 bool isPythonQtObjectPtrMetaId(int id) { return _PythonQtObjectPtr_metaId == id; }
694 bool isPythonQtSafeObjectPtrMetaId(int id) { return _PythonQtSafeObjectPtr_metaId == id; }
696 bool isPythonQtAnyObjectPtrMetaId(int id) { return _PythonQtObjectPtr_metaId == id || _PythonQtSafeObjectPtr_metaId == id; }
697
701 void removeWrapperPointer(void* obj);
702
704 void shellClassDeleted(void* shellClass);
705
707 void* unwrapForeignWrapper(const QByteArray& classname, PyObject* obj);
708
710 bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset);
711
714
717
719 void removeSignalEmitter(QObject* obj);
720
722 PyObject* wrapQObject(QObject* obj);
723
727 PyObject* wrapPtr(void* ptr, const QByteArray& name, bool passOwnership = false);
728
730 static PyObject* wrapMemoryAsBuffer(const void* data, Py_ssize_t size);
731
733 static PyObject* wrapMemoryAsBuffer(void* data, Py_ssize_t size);
734
736 /* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
737 you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
738 void registerClass(const QMetaObject* metaobject, const char* package = nullptr, PythonQtQObjectCreatorFunctionCB* wrapperCreator = nullptr, PythonQtShellSetInstanceWrapperCB* shell = nullptr, PyObject* module = nullptr, int typeSlots = 0);
739
742
748 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);
749
751 void registerGlobalNamespace(const char* typeName, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, const QMetaObject& metaObject, PyObject* module = nullptr);
752
755 void registerQObjectClassNames(const QStringList& names);
756
758 void addDecorators(QObject* o, int decoTypes);
759
761 PythonQtClassWrapper* createNewPythonQtClassWrapper(PythonQtClassInfo* info, PyObject* module, const QByteArray& pythonClassName);
762
764 static PyObject* createEnumValueInstance(PyObject* enumType, unsigned int enumValue);
765
767 static PyObject* createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject);
768
770 PythonQtInstanceWrapper* createNewPythonQtInstanceWrapper(QObject* obj, PythonQtClassInfo* info, void* wrappedPtr = nullptr);
771
773 PythonQtClassInfo* getClassInfo(const QMetaObject* meta);
774
776 PythonQtClassInfo* getClassInfo(const QByteArray& className);
777
780 void registerLazyClass(const QByteArray& name, const QByteArray& moduleToImport);
781
783 PythonQtObjectPtr createModule(const QString& name, PyObject* pycode);
784
787
790
792 void handleVirtualOverloadReturnError(const char* signature, const PythonQtMethodInfo* methodInfo, PyObject* result);
793
795 PythonQtObjectPtr pythonQtModule() const { return _pythonQtModule; }
796
798 PythonQt::ProfilingCB* profilingCB() const { return _profilingCB; }
799
801 QString getSignature(PyObject* object);
802
804 bool isMethodDescriptor(PyObject* object) const;
805
807 const QMetaObject* getDynamicMetaObject(PythonQtInstanceWrapper* wrapper, const QMetaObject* prototypeMetaObject);
808
810 const QMetaObject* setupDynamicMetaObjectChain(PythonQtClassWrapper* type, const QMetaObject* prototypeMetaObject);
811
813 const QMetaObject* buildDynamicMetaObject(PythonQtClassWrapper* type, const QMetaObject* prototypeMetaObject);
814
816 int handleMetaCall(QObject* object, PythonQtInstanceWrapper* wrapper, QMetaObject::Call call, int id, void** args);
817
819 void callMethodInPython(QMetaMethod &method, PythonQtInstanceWrapper* wrapper, void** args);
820
821private:
823 void setupSharedLibrarySuffixes();
824
826 void createPythonQtClassWrapper(PythonQtClassInfo* info, const char* package, PyObject* module = nullptr);
827
829 PyObject* packageByName(const char* name);
830
832 PythonQtInstanceWrapper* findWrapperAndRemoveUnused(void* obj);
833
835 QHash<void* , PythonQtInstanceWrapper *> _wrappedObjects;
836
838 QHash<QByteArray, PythonQtClassInfo *> _knownClassInfos;
839
841 QHash<QByteArray, bool> _knownQObjectClassNames;
842
844 QHash<QByteArray, QByteArray> _knownLazyClasses;
845
847 QHash<QObject* , PythonQtSignalReceiver *> _signalReceivers;
848
850 PythonQtObjectPtr _pythonQtModule;
851
853 QByteArray _pythonQtModuleName;
854
856 PythonQtImportFileInterface* _importInterface;
857
859 PythonQtQFileImporter* _defaultImporter;
860
861 PythonQtQObjectNoLongerWrappedCB* _noLongerWrappedCB;
862 PythonQtQObjectWrappedCB* _wrappedCB;
863 PythonQtQObjectMissingAttributeCB* _qObjectMissingAttribCB;
864
865 QStringList _importIgnorePaths;
866 QStringList _sharedLibrarySuffixes;
867
868 PythonQtObjectPtr _pySourceFileLoader;
869 PythonQtObjectPtr _pySourcelessFileLoader;
870 PythonQtObjectPtr _pyEnsureFuture;
871 PythonQtObjectPtr _pyFutureClass;
872
873 PythonQtObjectPtr _pyTaskDoneCallback;
874
876 QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
877
878 QList<PythonQtForeignWrapperFactory*> _foreignWrapperFactories;
879
880 QHash<QByteArray, PyObject*> _packages;
881
882 PythonQtClassInfo* _currentClassInfoForClassWrapperCreation;
883
884 PythonQt::ProfilingCB* _profilingCB;
885
886 PythonQtDebugAPI* _debugAPI;
887 PythonQtConfigAPI* _configAPI;
888
889 int _initFlags;
890 int _PythonQtObjectPtr_metaId;
891 int _PythonQtSafeObjectPtr_metaId;
892
893 bool _hadError;
894 bool _systemExitExceptionHandlerEnabled;
895
896 friend class PythonQt;
897};
898
899#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:138
QObject * PythonQtCreateObject()
helper template to create a derived QObject class
Definition PythonQt.h:147
void PythonQtQObjectWrappedCB(QObject *object)
Definition PythonQt.h:74
void PythonQtShellSetInstanceWrapperCB(void *object, PythonQtInstanceWrapper *wrapper)
Definition PythonQt.h:79
void * PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name)
Definition PythonQt.h:76
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 PythonQtSetInstanceWrapperOnShell(void *object, PythonQtInstanceWrapper *wrapper)
Definition PythonQt.h:81
QObject * PythonQtQObjectCreatorFunctionCB()
callback to create a QObject lazily
Definition PythonQt.h:144
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:662
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:689
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:795
bool isPythonQtSafeObjectPtrMetaId(int id)
returns if the id is the id for PythonQtSafeObjectPtr
Definition PythonQt.h:694
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:692
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:696
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:798
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:163
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:170
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:241
ProfilingCallbackState
enum for profiling callback
Definition PythonQt.h:214
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:178
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:544
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:562
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:221
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...