59 #include <QStringList>
82 (
reinterpret_cast<T*
>(object))->_wrapper = wrapper;
95 operator T()
const {
return _t; }
112 operator T()
const {
return _t; }
129 operator T()
const {
return _t; }
139 return ((
reinterpret_cast<char*
>(
static_cast<T2*
>(
reinterpret_cast<T1*
>(0x100))))
140 - (
reinterpret_cast<char*
>(
reinterpret_cast<T1*
>(0x100))));
151 #define QStringToPythonConstCharPointer(arg) ((arg).toUtf8().constData())
152 #define QStringToPythonCharPointer(arg) ((arg).toUtf8().data())
153 #define QStringToPythonEncoding(arg) ((arg).toUtf8())
155 #define QStringToPythonConstCharPointer(arg) ((arg).toLatin1().constData())
156 #define QStringToPythonCharPointer(arg) ((arg).toLatin1().data())
157 #define QStringToPythonEncoding(arg) ((arg).toLatin1())
178 IgnoreSiteModule = 2,
180 PythonAlreadyInitialized = 8
186 Type_Subtract = 1 << 1,
187 Type_Multiply = 1 << 2,
188 Type_Divide = 1 << 3,
193 Type_LShift = 1 << 8,
194 Type_RShift = 1 << 9,
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,
207 Type_Length = 1 << 20,
208 Type_MappingSetItem = 1 << 21,
209 Type_MappingGetItem = 1 << 22,
211 Type_EnterExit = 1 << 23,
213 Type_Invert = 1 << 29,
214 Type_RichCompare = 1 << 30,
215 Type_NonZero = 1 << 31,
236 static void init(
int flags = IgnoreSiteModule | RedirectStdOut,
const QByteArray& pythonQtModuleName = QByteArray());
442 QVariant
call(
PyObject*
object,
const QString& callable,
const QVariantList& args = QVariantList(),
const QVariantMap& kwargs = QVariantMap());
445 QVariant
call(
PyObject* callable,
const QVariantList& args = QVariantList(),
const QVariantMap& kwargs = QVariantMap());
630 void initPythonQtModule(
bool redirectStdOut,
const QByteArray& pythonQtModuleName);
632 QString getReturnTypeOfWrappedMethodHelper(
const PythonQtObjectPtr& variableObject,
const QString&
methodName,
const QString& context);
637 static void stdOutRedirectCB(
const QString& str);
639 static void stdErrRedirectCB(
const QString& str);
644 PythonQt(
int flags,
const QByteArray& pythonQtModuleName);
647 static int _uniqueModuleCount;
667 ConstructorDecorator = 2,
668 DestructorDecorator = 4,
669 InstanceDecorator = 8,
670 AllDecorators = 0xffff
818 void setupSharedLibrarySuffixes();
824 PyObject* packageByName(
const char* name);
830 QHash<void* , PythonQtInstanceWrapper *> _wrappedObjects;
833 QHash<QByteArray, PythonQtClassInfo *> _knownClassInfos;
836 QHash<QByteArray, bool> _knownQObjectClassNames;
839 QHash<QByteArray, QByteArray> _knownLazyClasses;
842 QHash<QObject* , PythonQtSignalReceiver *> _signalReceivers;
848 QByteArray _pythonQtModuleName;
860 QStringList _importIgnorePaths;
861 QStringList _sharedLibrarySuffixes;
871 QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
873 QList<PythonQtForeignWrapperFactory*> _foreignWrapperFactories;
875 QHash<QByteArray, PyObject*> _packages;
885 int _PythonQtObjectPtr_metaId;
886 int _PythonQtSafeObjectPtr_metaId;
889 bool _systemExitExceptionHandlerEnabled;
QString PythonQtInputChangedCB(void *callData)
declares the callback that is called from the write() function
int PythonQtUpcastingOffset()
returns the offset that needs to be added to upcast an object of type T1 to T2
QObject * PythonQtQObjectCreatorFunctionCB()
callback to create a QObject lazily
void PythonQtQObjectWrappedCB(QObject *object)
void PythonQtShellSetInstanceWrapperCB(void *object, PythonQtInstanceWrapper *wrapper)
QObject * PythonQtCreateObject()
helper template to create a derived QObject class
void PythonQtVoidPtrCB(void *object)
QString PythonQtQObjectMissingAttributeCB(QObject *object, const QString &attribute)
void PythonQtQObjectNoLongerWrappedCB(QObject *object)
void * PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name)
void PythonQtSetInstanceWrapperOnShell(void *object, PythonQtInstanceWrapper *wrapper)
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>
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>
PythonQtPassOwnershipToPython(const T &t)
Allow conversion from T to PythonQtPassOwnershipToPython<T>
internal PythonQt details
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
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
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
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
void registerLazyClass(const QByteArray &name, const QByteArray &moduleToImport)
PythonQt::ProfilingCB * profilingCB() const
returns the profiling callback, which may be NULL
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
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.
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()
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
ProfilingCallbackState
enum for profiling callback
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
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()
void addPolymorphicHandler(const char *typeName, PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
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)
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)
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)
QByteArray typeName(const QMetaMethod &method)
QByteArray signature(const QMetaMethod &method)
a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjec...