PythonQt
PythonQtObjectPtr Class Reference

a smart pointer that stores a PyObject pointer and that handles reference counting automatically More...

#include <PythonQtObjectPtr.h>

Public Member Functions

 PythonQtObjectPtr ()
 PythonQtObjectPtr (const PythonQtObjectPtr &p)
 PythonQtObjectPtr (PythonQtObjectPtr &&p) noexcept
 rvalue copy constructor, does not need any incref/decref.
 PythonQtObjectPtr (PythonQtSafeObjectPtr &&p)
 rvalue copy constructor, does not need any incref/decref.
 PythonQtObjectPtr (const QVariant &variant)
 If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count.
 PythonQtObjectPtr (PyObject *o)
 ~PythonQtObjectPtr ()
bool fromVariant (const QVariant &variant)
 If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count.
QVariant toVariant ()
 Returns a PythonQtSafeObjectPtr as a QVariant. It does not return a PythonQtObjectPtr, because that would be unsafe regarding the GIL.
QVariant toLocalVariant ()
 Returns a PythonQtObjectPtr as a QVariant. Only use this when you know that the variant is only used locally and not stored outside of the current GIL scope.
PythonQtObjectPtr & operator= (const PythonQtObjectPtr &p)
PythonQtObjectPtr & operator= (PythonQtObjectPtr &&p) noexcept
 rvalue assignment operator that steals the reference from p
PythonQtObjectPtr & operator= (PythonQtSafeObjectPtr &&p)
 rvalue assignment operator that steals the reference from p
PythonQtObjectPtr & operator= (PyObject *o)
PythonQtObjectPtr & operator= (const QVariant &variant)
bool operator== (const PythonQtObjectPtr &p) const
bool operator!= (const PythonQtObjectPtr &p) const
bool operator== (PyObject *p) const
bool operator!= (PyObject *p) const
bool isNull () const
PyObject * operator-> () const
PyObject & operator* () const
 operator PyObject * () const
void setNewRef (PyObject *o)
 sets the object and passes the ownership (stealing the reference, in Python slang)
PyObject * object () const
QVariant evalScript (const QString &script, int start=Py_file_input)
 evaluates the given script code in the context of this object and returns the result value
QVariant evalCode (PyObject *pycode)
void evalFile (const QString &filename)
 evaluates the given code in the context
void addObject (const QString &name, QObject *object)
 add the given object to the module as a variable with name (it can be removed via clearVariable)
void addVariable (const QString &name, const QVariant &v)
 add the given variable to the module
void removeVariable (const QString &name)
 remove the given variable
QVariant getVariable (const QString &name)
 get the variable with the name of the module, returns an invalid QVariant on error
QVariant call (const QString &callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
 call the given python object (in the scope of the current object), returns the result converted to a QVariant
QVariant call (const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
 call the contained python object directly, returns the result converted to a QVariant
PyObject * takeObject ()

Protected Member Functions

void setObject (PyObject *o)

Detailed Description

a smart pointer that stores a PyObject pointer and that handles reference counting automatically

Definition at line 56 of file PythonQtObjectPtr.h.

Constructor & Destructor Documentation

◆ PythonQtObjectPtr() [1/6]

PythonQtObjectPtr::PythonQtObjectPtr ( )
inline

Definition at line 59 of file PythonQtObjectPtr.h.

60 : _object(nullptr)
61 {
62 }

Referenced by operator!=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator==(), PythonQtObjectPtr(), and PythonQtObjectPtr().

◆ PythonQtObjectPtr() [2/6]

PythonQtObjectPtr::PythonQtObjectPtr ( const PythonQtObjectPtr & p)
inline

Definition at line 64 of file PythonQtObjectPtr.h.

65 : _object(nullptr)
66 {
67 setObject(p.object());
68 }
void setObject(PyObject *o)
PyObject * object() const

References object(), PythonQtObjectPtr(), and setObject().

◆ PythonQtObjectPtr() [3/6]

PythonQtObjectPtr::PythonQtObjectPtr ( PythonQtObjectPtr && p)
inlinenoexcept

rvalue copy constructor, does not need any incref/decref.

Definition at line 71 of file PythonQtObjectPtr.h.

72 : _object(p.takeObject())
73 {
74 }

References PythonQtObjectPtr().

◆ PythonQtObjectPtr() [4/6]

PythonQtObjectPtr::PythonQtObjectPtr ( PythonQtSafeObjectPtr && p)

rvalue copy constructor, does not need any incref/decref.

◆ PythonQtObjectPtr() [5/6]

PythonQtObjectPtr::PythonQtObjectPtr ( const QVariant & variant)
inline

If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count.

Definition at line 80 of file PythonQtObjectPtr.h.

81 : _object(nullptr)
82 {
83 fromVariant(variant);
84 }
bool fromVariant(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...

References fromVariant().

◆ PythonQtObjectPtr() [6/6]

PythonQtObjectPtr::PythonQtObjectPtr ( PyObject * o)

◆ ~PythonQtObjectPtr()

PythonQtObjectPtr::~PythonQtObjectPtr ( )

Member Function Documentation

◆ addObject()

void PythonQtObjectPtr::addObject ( const QString & name,
QObject * object )

add the given object to the module as a variable with name (it can be removed via clearVariable)

◆ addVariable()

void PythonQtObjectPtr::addVariable ( const QString & name,
const QVariant & v )

add the given variable to the module

◆ call() [1/2]

QVariant PythonQtObjectPtr::call ( const QString & callable,
const QVariantList & args = QVariantList(),
const QVariantMap & kwargs = QVariantMap() )

call the given python object (in the scope of the current object), returns the result converted to a QVariant

◆ call() [2/2]

QVariant PythonQtObjectPtr::call ( const QVariantList & args = QVariantList(),
const QVariantMap & kwargs = QVariantMap() )

call the contained python object directly, returns the result converted to a QVariant

◆ evalCode()

QVariant PythonQtObjectPtr::evalCode ( 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 PythonQtObjectPtr::evalFile ( const QString & filename)

evaluates the given code in the context

◆ evalScript()

QVariant PythonQtObjectPtr::evalScript ( const QString & script,
int start = Py_file_input )

evaluates the given script code in the context of this object and returns the result value

◆ fromVariant()

bool PythonQtObjectPtr::fromVariant ( const QVariant & variant)

If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference. This results in an increment of the reference count.

Referenced by operator=(), and PythonQtObjectPtr().

◆ getVariable()

QVariant PythonQtObjectPtr::getVariable ( const QString & name)

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

◆ isNull()

bool PythonQtObjectPtr::isNull ( ) const
inline

Definition at line 138 of file PythonQtObjectPtr.h.

138{ return !object(); }

References object().

◆ object()

◆ operator PyObject *()

PythonQtObjectPtr::operator PyObject * ( ) const
inline

Definition at line 144 of file PythonQtObjectPtr.h.

144{ return object(); }

References object().

◆ operator!=() [1/2]

bool PythonQtObjectPtr::operator!= ( const PythonQtObjectPtr & p) const
inline

Definition at line 132 of file PythonQtObjectPtr.h.

132{ return !(*this == p); }

References PythonQtObjectPtr().

◆ operator!=() [2/2]

bool PythonQtObjectPtr::operator!= ( PyObject * p) const
inline

Definition at line 136 of file PythonQtObjectPtr.h.

136{ return object() != p; }

References object().

◆ operator*()

PyObject & PythonQtObjectPtr::operator* ( ) const
inline

Definition at line 142 of file PythonQtObjectPtr.h.

142{ return *(object()); }

References object().

◆ operator->()

PyObject * PythonQtObjectPtr::operator-> ( ) const
inline

Definition at line 140 of file PythonQtObjectPtr.h.

140{ return object(); }

References object().

◆ operator=() [1/5]

PythonQtObjectPtr & PythonQtObjectPtr::operator= ( const PythonQtObjectPtr & p)
inline

Definition at line 99 of file PythonQtObjectPtr.h.

100 {
101 setObject(p.object());
102 return *this;
103 }

References object(), PythonQtObjectPtr(), and setObject().

◆ operator=() [2/5]

PythonQtObjectPtr & PythonQtObjectPtr::operator= ( const QVariant & variant)
inline

Definition at line 124 of file PythonQtObjectPtr.h.

125 {
126 fromVariant(variant);
127 return *this;
128 }

References fromVariant(), and PythonQtObjectPtr().

◆ operator=() [3/5]

PythonQtObjectPtr & PythonQtObjectPtr::operator= ( PyObject * o)
inline

Definition at line 118 of file PythonQtObjectPtr.h.

119 {
120 setObject(o);
121 return *this;
122 }

References PythonQtObjectPtr(), and setObject().

◆ operator=() [4/5]

PythonQtObjectPtr & PythonQtObjectPtr::operator= ( PythonQtObjectPtr && p)
inlinenoexcept

rvalue assignment operator that steals the reference from p

Definition at line 106 of file PythonQtObjectPtr.h.

107 {
108 if (_object) {
109 setObject(nullptr);
110 }
111 _object = p.takeObject();
112 return *this;
113 }

References PythonQtObjectPtr(), and setObject().

◆ operator=() [5/5]

PythonQtObjectPtr & PythonQtObjectPtr::operator= ( PythonQtSafeObjectPtr && p)

rvalue assignment operator that steals the reference from p

References PythonQtObjectPtr().

◆ operator==() [1/2]

bool PythonQtObjectPtr::operator== ( const PythonQtObjectPtr & p) const
inline

Definition at line 130 of file PythonQtObjectPtr.h.

130{ return object() == p.object(); }

References object(), and PythonQtObjectPtr().

◆ operator==() [2/2]

bool PythonQtObjectPtr::operator== ( PyObject * p) const
inline

Definition at line 134 of file PythonQtObjectPtr.h.

134{ return object() == p; }

References object().

◆ removeVariable()

void PythonQtObjectPtr::removeVariable ( const QString & name)

remove the given variable

◆ setNewRef()

void PythonQtObjectPtr::setNewRef ( PyObject * o)

sets the object and passes the ownership (stealing the reference, in Python slang)

◆ setObject()

void PythonQtObjectPtr::setObject ( PyObject * o)
protected

◆ takeObject()

PyObject * PythonQtObjectPtr::takeObject ( )
inline

takes the object from the pointer, leaving the pointer empty. the caller has to take care about the decref of the taken object!

Definition at line 182 of file PythonQtObjectPtr.h.

183 {
184 PyObject* o = _object;
185 _object = nullptr;
186 return o;
187 }
struct _object PyObject

◆ toLocalVariant()

QVariant PythonQtObjectPtr::toLocalVariant ( )

Returns a PythonQtObjectPtr as a QVariant. Only use this when you know that the variant is only used locally and not stored outside of the current GIL scope.

◆ toVariant()

QVariant PythonQtObjectPtr::toVariant ( )

Returns a PythonQtSafeObjectPtr as a QVariant. It does not return a PythonQtObjectPtr, because that would be unsafe regarding the GIL.


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