PythonQt
Public Types | List of all members
PythonQt Class Reference

The main interface to the Python Qt binding, realized as a singleton. More...

#include <PythonQt.h>

Inheritance diagram for PythonQt:

Public Types

enum  InitFlags { RedirectStdOut = 1 , IgnoreSiteModule = 2 , ExternalHelp = 4 , PythonAlreadyInitialized = 8 }
 flags that can be passed to PythonQt::init() More...
 
enum  TypeSlots {
  Type_Add = 1 , Type_Subtract = 1 << 1 , Type_Multiply = 1 << 2 , Type_Divide = 1 << 3 ,
  Type_Mod = 1 << 4 , Type_And = 1 << 5 , Type_Or = 1 << 6 , Type_Xor = 1 << 7 ,
  Type_LShift = 1 << 8 , Type_RShift = 1 << 9 , Type_InplaceAdd = 1 << 10 , Type_InplaceSubtract = 1 << 11 ,
  Type_InplaceMultiply = 1 << 12 , Type_InplaceDivide = 1 << 13 , Type_InplaceMod = 1 << 14 , Type_InplaceAnd = 1 << 15 ,
  Type_InplaceOr = 1 << 16 , Type_InplaceXor = 1 << 17 , Type_InplaceLShift = 1 << 18 , Type_InplaceRShift = 1 << 19 ,
  Type_Length = 1 << 20 , Type_MappingSetItem = 1 << 21 , Type_MappingGetItem = 1 << 22 , Type_EnterExit = 1 << 23 ,
  Type_Invert = 1 << 29 , Type_RichCompare = 1 << 30 , Type_NonZero = 1 << 31
}
 flags that tell PythonQt which operators to expect on the registered type More...
 
enum  ProfilingCallbackState { Enter = 1 , Leave = 2 }
 enum for profiling callback More...
 
typedef void ProfilingCB(ProfilingCallbackState state, const char *className, const char *methodName, PyObject *args)
 

Public Member Functions

Standard input handling
void setRedirectStdInCallback (PythonQtInputChangedCB *callback, void *callbackData=nullptr)
 
void setRedirectStdInCallbackEnabled (bool enabled)
 
Modules
PythonQtObjectPtr getMainModule ()
 get the main module of python More...
 
PythonQtObjectPtr importModule (const QString &name)
 
PythonQtObjectPtr createModuleFromFile (const QString &name, const QString &filename)
 
PythonQtObjectPtr createModuleFromScript (const QString &name, const QString &script=QString())
 
PythonQtObjectPtr createUniqueModule ()
 
Importing/Paths
void overwriteSysPath (const QStringList &paths)
 overwrite the python sys path (call this directly after PythonQt::init() if you want to change the std python sys path) More...
 
void addSysPath (const QString &path)
 prepend a path to sys.path to allow importing from it More...
 
void setModuleImportPath (PyObject *module, const QStringList &paths)
 sets the path list of a module to the given list (important for local imports) More...
 
Registering Classes
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) More...
 
void registerCPPClass (const char *typeName, const char *parentTypeName=nullptr, const char *package=nullptr, PythonQtQObjectCreatorFunctionCB *wrapperCreator=nullptr, PythonQtShellSetInstanceWrapperCB *shell=nullptr)
 
void registerQObjectClassNames (const QStringList &names)
 
bool addParentClass (const char *typeName, const char *parentTypeName, int upcastingOffset=0)
 
void addPolymorphicHandler (const char *typeName, PythonQtPolymorphicHandlerCB *cb)
 add a handler for polymorphic downcasting More...
 
Script Parsing and Evaluation
PythonQtObjectPtr parseFile (const QString &filename)
 parses the given file (using PythonQt's own import mechanism) and returns the python code object, this can then be used to call evalCode() More...
 
PythonQtObjectPtr parseFileWithPythonLoaders (const QString &filename)
 
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 More...
 
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 More...
 
void evalFile (PyObject *object, const QString &filename)
 evaluates the given script code from file More...
 
Signal Handlers
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 More...
 
bool removeSignalHandler (QObject *obj, const char *signal, PyObject *module, const QString &objectname)
 remove a signal handler from the given signal of obj More...
 
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 More...
 
bool removeSignalHandler (QObject *obj, const char *signal, PyObject *receiver)
 remove a signal handler from the given signal of obj More...
 
void removeSignalHandlers ()
 globally removes all signal handlers (connections between QObjects and Python). More...
 
Variable access
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 clearVariable) More...
 
void addVariable (PyObject *object, const QString &name, const QVariant &v)
 add the given variable to the object More...
 
void removeVariable (PyObject *module, const QString &name)
 remove the given variable More...
 
QVariant getVariable (PyObject *object, const QString &name)
 get the variable with the name of the object, returns an invalid QVariant on error More...
 
QVariant getNativeVariable (PyObject *object, const QString &name)
 get the variable with the name of the object as QVariant of type PythonQtObjectPtr, returns an invalid QVariant on error More...
 
QStringList introspection (PyObject *object, const QString &objectname, ObjectType type)
 read vars etc. in scope of an object, optional looking inside of an object objectname More...
 
QStringList introspectObject (PyObject *object, ObjectType type)
 read vars etc. in scope of the given object More...
 
QStringList introspectType (const QString &typeName, ObjectType type)
 
PythonQtObjectPtr lookupCallable (PyObject *object, const QString &name)
 
QString getReturnTypeOfWrappedMethod (PyObject *module, const QString &objectname)
 returns the return type of the method of a wrapped c++ object referenced by objectname More...
 
QString getReturnTypeOfWrappedMethod (const QString &typeName, const QString &methodName)
 returns the return type of the method methodName of a wrapped c++ type referenced by typeName More...
 
Calling Python Objects
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 More...
 
QVariant call (PyObject *callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
 call the given python object, returns the result converted to a QVariant More...
 
PyObjectcallAndReturnPyObject (PyObject *callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
 call the given python object, returns the result as new PyObject More...
 
Decorations, Constructors, Wrappers...
void addInstanceDecorators (QObject *o)
 
void addClassDecorators (QObject *o)
 
void addDecorators (QObject *o)
 this will add the object both as class and instance decorator (ownership is passed to PythonQt) More...
 
void addWrapperFactory (PythonQtCppWrapperFactory *factory)
 add the given factory to PythonQt (ownership stays with caller) More...
 
void addWrapperFactory (PythonQtForeignWrapperFactory *factory)
 add the given factory to PythonQt (ownership stays with caller) More...
 
void removeWrapperFactory (PythonQtCppWrapperFactory *factory)
 remove the wrapper factory More...
 
void removeWrapperFactory (PythonQtForeignWrapperFactory *factory)
 remove the wrapper factory More...
 

Singleton Initialization

enum  ObjectType {
  Class , Function , Variable , Module ,
  Anything , CallOverloads
}
 defines the object types for introspection More...
 
static void init (int flags=IgnoreSiteModule|RedirectStdOut, const QByteArray &pythonQtModuleName=QByteArray())
 
static void cleanup ()
 cleanup of the singleton More...
 
static PythonQtself ()
 get the singleton instance More...
 

Other Stuff

static PythonQtPrivatepriv ()
 get access to internal data (should not be used on the public API, but is used by some C functions) More...
 
static void qObjectNoLongerWrappedCB (QObject *o)
 call the callback if it is set More...
 
static QString qObjectMissingAttributeCallback (QObject *o, const QString &attribute)
 call the callback if it is set More...
 
static void setEnableThreadSupport (bool flag)
 
void clearNotFoundCachedMembers ()
 
bool handleError (bool printStack=true)
 
bool hadError () const
 return true if handleError() has been called and an error occurred. More...
 
void clearError ()
 
void setSystemExitExceptionHandlerEnabled (bool value)
 
bool systemExitExceptionHandlerEnabled () const
 
void setQObjectWrappedCallback (PythonQtQObjectWrappedCB *cb)
 set a callback that is called when a QObject with parent == NULL is wrapped by PythonQt More...
 
void setQObjectNoLongerWrappedCallback (PythonQtQObjectNoLongerWrappedCB *cb)
 set a callback that is called when a QObject with parent == NULL is no longer wrapped by PythonQt More...
 
void setQObjectMissingAttributeCallback (PythonQtQObjectMissingAttributeCB *cb)
 set a callback that is called when a QObject does not have a specific attribute. More...
 
PyObjecthelpCalled (PythonQtClassInfo *info)
 called by internal help methods More...
 
PythonQtObjectPtr lookupObject (PyObject *module, const QString &name)
 
void setProfilingCallback (ProfilingCB *cb)
 sets a callback that is called before and after function calls for profiling More...
 
void pythonStdOut (const QString &str)
 emitted when python outputs something to stdout (and redirection is turned on) More...
 
void pythonStdErr (const QString &str)
 emitted when python outputs something to stderr (and redirection is turned on) More...
 
void pythonHelpRequest (const QByteArray &cppClassName)
 emitted when help() is called on a PythonQt object and ExternalHelp is enabled More...
 
void systemExitExceptionRaised (int exitCode)
 

Custom Importer

static PythonQtImportFileInterfaceimportInterface ()
 get access to the file importer (if set) More...
 
void setImporter (PythonQtImportFileInterface *importInterface)
 
void installDefaultImporter ()
 
void setImporterIgnorePaths (const QStringList &paths)
 set paths that the importer should ignore More...
 
const QStringList & getImporterIgnorePaths ()
 get paths that the importer should ignore More...
 

Detailed Description

The main interface to the Python Qt binding, realized as a singleton.

Use PythonQt::init() to initialize the singleton and PythonQt::self() to access it. While there can be only one PythonQt instance, you can have any number of Python context to do scripting in. One possibility is to use createModuleFromFile(), createModuleFromScript() or createUniqueModule() to get a context that is separated from the other contexts. Alternatively you can use Python dicts as contexts for script evaluation, but you will need to populate the dict with the builtins instance to have all Pythons available when running code in the scope of a dict.

Definition at line 169 of file PythonQt.h.

Member Typedef Documentation

◆ ProfilingCB

typedef void PythonQt::ProfilingCB(ProfilingCallbackState state, const char *className, const char *methodName, PyObject *args)

callback for profiling. className and methodName are only passed when state == Enter, otherwise they are NULL.

Definition at line 227 of file PythonQt.h.

Member Enumeration Documentation

◆ InitFlags

flags that can be passed to PythonQt::init()

Enumerator
RedirectStdOut 

< sets if the std out/err is redirected to pythonStdOut() and pythonStdErr() signals

IgnoreSiteModule 

< sets if Python should ignore the site module

ExternalHelp 

< sets if help() calls on PythonQt modules are forwarded to the pythonHelpRequest() signal

PythonAlreadyInitialized 

< sets that PythonQt should not can PyInitialize, since it is already done

Definition at line 176 of file PythonQt.h.

176  {
177  RedirectStdOut = 1,
178  IgnoreSiteModule = 2,
179  ExternalHelp = 4,
181  };
@ RedirectStdOut
< sets if the std out/err is redirected to pythonStdOut() and pythonStdErr() signals
Definition: PythonQt.h:177
@ IgnoreSiteModule
< sets if Python should ignore the site module
Definition: PythonQt.h:178
@ PythonAlreadyInitialized
< sets that PythonQt should not can PyInitialize, since it is already done
Definition: PythonQt.h:180
@ ExternalHelp
< sets if help() calls on PythonQt modules are forwarded to the pythonHelpRequest() signal
Definition: PythonQt.h:179

◆ ObjectType

defines the object types for introspection

Enumerator
Class 
Function 
Variable 
Module 
Anything 
CallOverloads 

Definition at line 247 of file PythonQt.h.

247  {
248  Class,
249  Function,
250  Variable,
251  Module,
252  Anything,
254  };
@ Anything
Definition: PythonQt.h:252
@ Module
Definition: PythonQt.h:251
@ Variable
Definition: PythonQt.h:250
@ CallOverloads
Definition: PythonQt.h:253
@ Function
Definition: PythonQt.h:249

◆ ProfilingCallbackState

enum for profiling callback

Enumerator
Enter 
Leave 

Definition at line 220 of file PythonQt.h.

220  {
221  Enter = 1,
222  Leave = 2
223  };

◆ TypeSlots

flags that tell PythonQt which operators to expect on the registered type

Enumerator
Type_Add 
Type_Subtract 
Type_Multiply 
Type_Divide 
Type_Mod 
Type_And 
Type_Or 
Type_Xor 
Type_LShift 
Type_RShift 
Type_InplaceAdd 
Type_InplaceSubtract 
Type_InplaceMultiply 
Type_InplaceDivide 
Type_InplaceMod 
Type_InplaceAnd 
Type_InplaceOr 
Type_InplaceXor 
Type_InplaceLShift 
Type_InplaceRShift 
Type_Length 
Type_MappingSetItem 
Type_MappingGetItem 
Type_EnterExit 
Type_Invert 
Type_RichCompare 
Type_NonZero 

Definition at line 184 of file PythonQt.h.

184  {
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  };
@ Type_InplaceXor
Definition: PythonQt.h:203
@ Type_MappingGetItem
Definition: PythonQt.h:209
@ Type_InplaceAnd
Definition: PythonQt.h:201
@ Type_InplaceMultiply
Definition: PythonQt.h:198
@ Type_Length
Definition: PythonQt.h:207
@ Type_InplaceOr
Definition: PythonQt.h:202
@ Type_Add
Definition: PythonQt.h:185
@ Type_InplaceDivide
Definition: PythonQt.h:199
@ Type_EnterExit
Definition: PythonQt.h:211
@ Type_MappingSetItem
Definition: PythonQt.h:208
@ Type_RShift
Definition: PythonQt.h:194
@ Type_Xor
Definition: PythonQt.h:192
@ Type_Or
Definition: PythonQt.h:191
@ Type_Multiply
Definition: PythonQt.h:187
@ Type_And
Definition: PythonQt.h:190
@ Type_InplaceLShift
Definition: PythonQt.h:204
@ Type_Divide
Definition: PythonQt.h:188
@ Type_InplaceSubtract
Definition: PythonQt.h:197
@ Type_RichCompare
Definition: PythonQt.h:214
@ Type_InplaceMod
Definition: PythonQt.h:200
@ Type_InplaceRShift
Definition: PythonQt.h:205
@ Type_InplaceAdd
Definition: PythonQt.h:196
@ Type_Subtract
Definition: PythonQt.h:186
@ Type_Mod
Definition: PythonQt.h:189
@ Type_Invert
Definition: PythonQt.h:213
@ Type_LShift
Definition: PythonQt.h:193
@ Type_NonZero
Definition: PythonQt.h:215

Member Function Documentation

◆ addClassDecorators()

void PythonQt::addClassDecorators ( QObject *  o)

add an object whose slots will be used as decorator slots for class objects (ownership is passed to PythonQt)

The slots need to follow the following convention:

  • SomeClass* new_SomeClass(...)
  • QVariant new_SomeClass(...)
  • void delete_SomeClass(SomeClass*)
  • ... static_SomeClass_someName(...)

This will add:

  • a constructor
  • a constructor which generates a QVariant
  • a destructor (only useful for CPP objects)
  • a static decorator slot which will be available on the MetaObject (visible in PythonQt module)

◆ addDecorators()

void PythonQt::addDecorators ( QObject *  o)

this will add the object both as class and instance decorator (ownership is passed to PythonQt)

◆ addInstanceDecorators()

void PythonQt::addInstanceDecorators ( QObject *  o)

add an object whose slots will be used as decorator slots for other QObjects or CPP classes. The slots need to follow the convention that the first argument is a pointer to the wrapped object. (ownership is passed to PythonQt)

Example:

A slot with the signature

bool doSomething(QWidget* w, int a)

will extend QWidget instances (and derived classes) with a "bool doSomething(int a)" slot that will be called with the concrete instance as first argument. So in Python you can now e.g. call

someWidget.doSomething(12)

without QWidget really having this method. This allows to easily make normal methods of Qt classes callable by forwarding them with such decorator slots or to make CPP classes (which are not derived from QObject) callable from Python.

◆ addObject()

void PythonQt::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 clearVariable)

◆ addParentClass()

bool PythonQt::addParentClass ( const char *  typeName,
const char *  parentTypeName,
int  upcastingOffset = 0 
)

add a parent class relation to the given typeName, the upcastingOffset is needed for multiple inheritance and can be calculated using PythonQtUpcastingOffset<type,parentType>(), which also verifies that type is really derived from parentType. Returns false if the typeName was not yet registered.

◆ addPolymorphicHandler()

void PythonQt::addPolymorphicHandler ( const char *  typeName,
PythonQtPolymorphicHandlerCB cb 
)

add a handler for polymorphic downcasting

◆ addSignalHandler() [1/2]

bool PythonQt::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

◆ addSignalHandler() [2/2]

bool PythonQt::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

◆ addSysPath()

void PythonQt::addSysPath ( const QString &  path)

prepend a path to sys.path to allow importing from it

◆ addVariable()

void PythonQt::addVariable ( PyObject object,
const QString &  name,
const QVariant &  v 
)

add the given variable to the object

◆ addWrapperFactory() [1/2]

void PythonQt::addWrapperFactory ( PythonQtCppWrapperFactory factory)

add the given factory to PythonQt (ownership stays with caller)

◆ addWrapperFactory() [2/2]

void PythonQt::addWrapperFactory ( PythonQtForeignWrapperFactory factory)

add the given factory to PythonQt (ownership stays with caller)

◆ call() [1/2]

QVariant PythonQt::call ( PyObject callable,
const QVariantList &  args = QVariantList(),
const QVariantMap &  kwargs = QVariantMap() 
)

call the given python object, returns the result converted to a QVariant

◆ call() [2/2]

QVariant PythonQt::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

◆ callAndReturnPyObject()

PyObject* PythonQt::callAndReturnPyObject ( PyObject callable,
const QVariantList &  args = QVariantList(),
const QVariantMap &  kwargs = QVariantMap() 
)

call the given python object, returns the result as new PyObject

◆ cleanup()

static void PythonQt::cleanup ( )
static

cleanup of the singleton

◆ clearError()

void PythonQt::clearError ( )

reset error flag. After calling this, hadError() will return false.

See also
hadError()

◆ clearNotFoundCachedMembers()

void PythonQt::clearNotFoundCachedMembers ( )

clear all NotFound entries on all class infos, to ensure that newly loaded wrappers can add methods even when the object was wrapped by PythonQt before the wrapper was loaded

◆ createModuleFromFile()

PythonQtObjectPtr PythonQt::createModuleFromFile ( const QString &  name,
const QString &  filename 
)

creates the new module name and evaluates the given file in the context of that module If the script is empty, the module contains no initial code. You can use evalScript/evalCode to add code to a module later on. The user needs to make sure that the name is unique in the python module dictionary.

◆ createModuleFromScript()

PythonQtObjectPtr PythonQt::createModuleFromScript ( const QString &  name,
const QString &  script = QString() 
)

creates the new module name and evaluates the given script in the context of that module. If the script is empty, the module contains no initial code. You can use evalScript/evalCode to add code to a module later on. The user needs to make sure that the name is unique in the python module dictionary.

◆ createUniqueModule()

PythonQtObjectPtr PythonQt::createUniqueModule ( )

create a uniquely named module, you can use evalFile or evalScript to populate the module with script code

◆ evalCode()

QVariant PythonQt::evalCode ( PyObject object,
PyObject pycode 
)

evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string) If pycode is NULL, a python error is printed.

◆ evalFile()

void PythonQt::evalFile ( PyObject object,
const QString &  filename 
)

evaluates the given script code from file

◆ evalScript() [1/2]

QVariant PythonQt::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

◆ evalScript() [2/2]

QVariant PythonQt::evalScript ( PyObject object,
const QString &  script,
int  start = Py_file_input 
)

evaluates the given script code and returns the result value

◆ getImporterIgnorePaths()

const QStringList& PythonQt::getImporterIgnorePaths ( )

get paths that the importer should ignore

◆ getMainModule()

PythonQtObjectPtr PythonQt::getMainModule ( )

get the main module of python

◆ getNativeVariable()

QVariant PythonQt::getNativeVariable ( PyObject object,
const QString &  name 
)

get the variable with the name of the object as QVariant of type PythonQtObjectPtr, returns an invalid QVariant on error

◆ getReturnTypeOfWrappedMethod() [1/2]

QString PythonQt::getReturnTypeOfWrappedMethod ( const QString &  typeName,
const QString &  methodName 
)

returns the return type of the method methodName of a wrapped c++ type referenced by typeName

◆ getReturnTypeOfWrappedMethod() [2/2]

QString PythonQt::getReturnTypeOfWrappedMethod ( PyObject module,
const QString &  objectname 
)

returns the return type of the method of a wrapped c++ object referenced by objectname

◆ getVariable()

QVariant PythonQt::getVariable ( PyObject object,
const QString &  name 
)

get the variable with the name of the object, returns an invalid QVariant on error

◆ hadError()

bool PythonQt::hadError ( ) const

return true if handleError() has been called and an error occurred.

◆ handleError()

bool PythonQt::handleError ( bool  printStack = true)

handle a python error, call this when a python function fails. If no error occurred, it returns false. The error is currently just output to the python stderr, future version might implement better trace printing

◆ helpCalled()

PyObject* PythonQt::helpCalled ( PythonQtClassInfo info)

called by internal help methods

◆ importInterface()

static PythonQtImportFileInterface* PythonQt::importInterface ( )
static

get access to the file importer (if set)

◆ importModule()

PythonQtObjectPtr PythonQt::importModule ( const QString &  name)

import the given module and return a reference to it (useful to import e.g. "sys" and call something on it) If a module is already imported, this returns the already imported module.

◆ init()

static void PythonQt::init ( int  flags = IgnoreSiteModule|RedirectStdOut,
const QByteArray &  pythonQtModuleName = QByteArray() 
)
static

initialize the python qt binding (flags are a or combination of PythonQt::InitFlags), if pythonQtModuleName is given it defines the name of the python module that PythonQt will add, otherwise "PythonQt" is used. This can be used to e.g. pass in PySide or PyQt4 to make it more compatible.

◆ installDefaultImporter()

void PythonQt::installDefaultImporter ( )
inline

this installs the default QFile importer (which effectively does a setImporter(NULL)) (without calling setImporter or installDefaultImporter at least once, the default python import mechanism is in place) the default importer allows to import files from anywhere QFile can read from, including the Qt resource system using ":". Keep in mind that you need to extend "sys.path" with ":" to be able to import from the Qt resources.

Definition at line 539 of file PythonQt.h.

539 { setImporter(nullptr); }
void setImporter(PythonQtImportFileInterface *importInterface)

◆ introspection()

QStringList PythonQt::introspection ( PyObject object,
const QString &  objectname,
ObjectType  type 
)

read vars etc. in scope of an object, optional looking inside of an object objectname

◆ introspectObject()

QStringList PythonQt::introspectObject ( PyObject object,
ObjectType  type 
)

read vars etc. in scope of the given object

◆ introspectType()

QStringList PythonQt::introspectType ( const QString &  typeName,
ObjectType  type 
)

read vars etc. in scope of the type object called typename. First the typename of the form module.type is split into module and type. Then the module is looked up in sys.modules. If the module or type is not found there, then the type is looked up in the builtin module.

◆ lookupCallable()

PythonQtObjectPtr PythonQt::lookupCallable ( PyObject object,
const QString &  name 
)

returns the found callable object or NULL

Returns
new reference

◆ lookupObject()

PythonQtObjectPtr PythonQt::lookupObject ( PyObject module,
const QString &  name 
)

returns the found object or NULL

Returns
new reference

◆ overwriteSysPath()

void PythonQt::overwriteSysPath ( const QStringList &  paths)

overwrite the python sys path (call this directly after PythonQt::init() if you want to change the std python sys path)

◆ parseFile()

PythonQtObjectPtr PythonQt::parseFile ( const QString &  filename)

parses the given file (using PythonQt's own import mechanism) and returns the python code object, this can then be used to call evalCode()

◆ parseFileWithPythonLoaders()

PythonQtObjectPtr PythonQt::parseFileWithPythonLoaders ( const QString &  filename)

Parses the given file and returns the python code object, this can then be used to call evalCode() It uses Python's importlib machinery to load the file's code and supports source and sourceless loading and generation of cache files. This method is PY3K only!

◆ priv()

static PythonQtPrivate* PythonQt::priv ( )
inlinestatic

get access to internal data (should not be used on the public API, but is used by some C functions)

Definition at line 557 of file PythonQt.h.

557 { return _self->_p; }

Referenced by PythonQtConvertListOfKnownClassToPythonList(), and PythonQtConvertPythonListToListOfKnownClass().

◆ pythonHelpRequest

void PythonQt::pythonHelpRequest ( const QByteArray &  cppClassName)
signal

emitted when help() is called on a PythonQt object and ExternalHelp is enabled

◆ pythonStdErr

void PythonQt::pythonStdErr ( const QString &  str)
signal

emitted when python outputs something to stderr (and redirection is turned on)

◆ pythonStdOut

void PythonQt::pythonStdOut ( const QString &  str)
signal

emitted when python outputs something to stdout (and redirection is turned on)

◆ qObjectMissingAttributeCallback()

static QString PythonQt::qObjectMissingAttributeCallback ( QObject *  o,
const QString &  attribute 
)
static

call the callback if it is set

◆ qObjectNoLongerWrappedCB()

static void PythonQt::qObjectNoLongerWrappedCB ( QObject *  o)
static

call the callback if it is set

◆ registerClass()

void PythonQt::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)

◆ registerCPPClass()

void PythonQt::registerCPPClass ( const char *  typeName,
const char *  parentTypeName = nullptr,
const char *  package = nullptr,
PythonQtQObjectCreatorFunctionCB wrapperCreator = nullptr,
PythonQtShellSetInstanceWrapperCB shell = nullptr 
)

add a wrapper object for the given QMetaType typeName, also does an addClassDecorators() to add constructors for variants (ownership of wrapper is passed to PythonQt)

Make sure that you have done a qRegisterMetaType first, if typeName is a user type!

This will add a wrapper object that is used to make calls to the given classname typeName. All slots that take a pointer to typeName as the first argument will be callable from Python on a variant object that contains such a type.

◆ registerQObjectClassNames()

void PythonQt::registerQObjectClassNames ( const QStringList &  names)

as an alternative to registerClass, you can tell PythonQt the names of QObject derived classes and it will register the classes when it first sees a pointer to such a derived class

◆ removeSignalHandler() [1/2]

bool PythonQt::removeSignalHandler ( QObject *  obj,
const char *  signal,
PyObject module,
const QString &  objectname 
)

remove a signal handler from the given signal of obj

◆ removeSignalHandler() [2/2]

bool PythonQt::removeSignalHandler ( QObject *  obj,
const char *  signal,
PyObject receiver 
)

remove a signal handler from the given signal of obj

◆ removeSignalHandlers()

void PythonQt::removeSignalHandlers ( )

globally removes all signal handlers (connections between QObjects and Python).

◆ removeVariable()

void PythonQt::removeVariable ( PyObject module,
const QString &  name 
)

remove the given variable

◆ removeWrapperFactory() [1/2]

void PythonQt::removeWrapperFactory ( PythonQtCppWrapperFactory factory)

remove the wrapper factory

◆ removeWrapperFactory() [2/2]

void PythonQt::removeWrapperFactory ( PythonQtForeignWrapperFactory factory)

remove the wrapper factory

◆ self()

static PythonQt* PythonQt::self ( )
static

get the singleton instance

◆ setEnableThreadSupport()

static void PythonQt::setEnableThreadSupport ( bool  flag)
static

Enable GIL and thread state handling (turned off by default). If you want to use Python threading, you have to call this with true early in your main thread, before you launch any threads in Python. It can be called before or after PythonQt::init().

◆ setImporter()

void PythonQt::setImporter ( PythonQtImportFileInterface importInterface)

replace the internal import implementation and use the supplied interface to load files (both py and pyc files) (this method should be called directly after initialization of init() and before calling overwriteSysPath(). On the first call to this method, it will install a generic PythonQt importer in Pythons "path_hooks". This is not reversible, so even setting setImporter(NULL) afterwards will keep the custom PythonQt importer with a QFile default import interface. Subsequent python import calls will make use of the passed importInterface which forwards all import calls to the given importInterface. Passing NULL will install a default QFile importer. (importInterface ownership stays with caller)

◆ setImporterIgnorePaths()

void PythonQt::setImporterIgnorePaths ( const QStringList &  paths)

set paths that the importer should ignore

◆ setModuleImportPath()

void PythonQt::setModuleImportPath ( PyObject module,
const QStringList &  paths 
)

sets the path list of a module to the given list (important for local imports)

◆ setProfilingCallback()

void PythonQt::setProfilingCallback ( ProfilingCB cb)

sets a callback that is called before and after function calls for profiling

◆ setQObjectMissingAttributeCallback()

void PythonQt::setQObjectMissingAttributeCallback ( PythonQtQObjectMissingAttributeCB cb)

set a callback that is called when a QObject does not have a specific attribute.

◆ setQObjectNoLongerWrappedCallback()

void PythonQt::setQObjectNoLongerWrappedCallback ( PythonQtQObjectNoLongerWrappedCB cb)

set a callback that is called when a QObject with parent == NULL is no longer wrapped by PythonQt

◆ setQObjectWrappedCallback()

void PythonQt::setQObjectWrappedCallback ( PythonQtQObjectWrappedCB cb)

set a callback that is called when a QObject with parent == NULL is wrapped by PythonQt

◆ setRedirectStdInCallback()

void PythonQt::setRedirectStdInCallback ( PythonQtInputChangedCB callback,
void *  callbackData = nullptr 
)

Overwrite default handling of stdin using a custom callback. It internally backup the original 'sys.stdin' into 'sys.pythonqt_original_stdin'

◆ setRedirectStdInCallbackEnabled()

void PythonQt::setRedirectStdInCallbackEnabled ( bool  enabled)

Enable or disable stdin custom callback. It resets 'sys.stdin' using either 'sys.pythonqt_stdin' or 'sys.pythonqt_original_stdin'

◆ setSystemExitExceptionHandlerEnabled()

void PythonQt::setSystemExitExceptionHandlerEnabled ( bool  value)

if set to true, the systemExitExceptionRaised signal will be emitted if exception SystemExit is caught

See also
handleError()

◆ systemExitExceptionHandlerEnabled()

bool PythonQt::systemExitExceptionHandlerEnabled ( ) const

return true if SystemExit exception is handled by PythonQt

See also
setSystemExitExceptionHandlerEnabled()

◆ systemExitExceptionRaised

void PythonQt::systemExitExceptionRaised ( int  exitCode)
signal

emitted when both custom SystemExit exception handler is enabled and a SystemExit exception is raised.

See also
setSystemExitExceptionHandlerEnabled(bool)

The documentation for this class was generated from the following file: