Signal Slot Python Qt
For instance, one event activates its slot and related subsequent events trigger another signal and the code in its slot to be executed. General understanding of the python programming language. No prior knowledge of QT is required. Connecting signals and slots. We use the QObject.connect method to connect signals and slots. Qt allows a signal to be connected to a slot that requires fewer arguments than The extra arguments are quietly discarded. Be used in the same way. Note that when a slot is a Python callable its reference count is not. Traditional syntax: SIGNAL and SLOT QtCore.SIGNAL and QtCore.SLOT macros allow Python to interface with Qt signal and slot delivery mechanisms. This is the old way of using signals and slots. The example below uses the well known clicked signal from a QPushButton.The connect method has a non python-friendly syntax. NEW PYTHON DEVELOPMENT COURSE! CLICK HERE For more information and code download please visit Video 12 - We.
The main interface to PythonQt is the PythonQt singleton. PythonQt needs to be initialized via PythonQt::init() once. Afterwards you communicate with the singleton via PythonQt::self(). PythonQt offers a complete Qt binding, which needs to be enabled via PythonQt_QtAll::init().
Each PyQt widget, which is derived from QObject class, is designed to emit ‘ signal ’ in response to one or more events. The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘ slot ’. The slot can be any callable Python function.
The following table shows the mapping between Python and Qt objects:
Qt/C++ | Python |
---|---|
bool | bool |
double | float |
float | float |
char/uchar,int/uint,short,ushort,QChar | integer |
long | integer |
ulong,longlong,ulonglong | long |
QString | unicode string |
QByteArray | QByteArray wrapper (1) |
char* | str |
QStringList | tuple of unicode strings |
QVariantList | tuple of objects |
QVariantMap | dict of objects |
QVariant | depends on type (2) |
QSize, QRect and all other standard Qt QVariants | variant wrapper that supports complete API of the respective Qt classes |
OwnRegisteredMetaType | C++ wrapper, optionally with additional information/wrapping provided by registerCPPClass() |
QList<AnyObject*> | converts to a list of CPP wrappers |
QVector<AnyObject*> | converts to a list of CPP wrappers |
EnumType | Enum wrapper derived from python integer |
QObject (and derived classes) | QObject wrapper |
C++ object | CPP wrapper, either wrapped via PythonQtCppWrapperFactory or just decorated with decorators |
PyObject | PyObject (3) |
- The Python 'bytes' type will automatically be converted to QByteArray where required. For converting a QByteArray to 'bytes' use the .data() method.
- QVariants are mapped recursively as given above, e.g. a dictionary can contain lists of dictionaries of doubles.
- PyObject is passed as direct pointer, which allows to pass/return any Python object directly to/from a Qt slot that uses PyObject* as its argument/return value.
All Qt QVariant types are implemented, PythonQt supports the complete Qt API for these objects.
All classes derived from QObject are automatically wrapped with a python wrapper class when they become visible to the Python interpreter. This can happen via
- the PythonQt::addObject() method
- when a Qt slot returns a QObject derived object to python
- when a Qt signal contains a QObject and is connected to a python function
It is important that you call PythonQt::registerClass() for any QObject derived class that may become visible to Python, except when you add it via PythonQt::addObject(). This will register the complete parent hierachy of the registered class, so that when you register e.g. a QPushButton, QWidget will be registered as well (and all intermediate parents).
From Python, you can talk to the returned QObjects in a natural way by calling their slots and receiving the return values. You can also read/write all properties of the objects as if they where normal python properties.
In addition to this, the wrapped objects support
- className() - returns a string that reprents the classname of the QObject
- help() - shows all properties, slots, enums, decorator slots and constructors of the object, in a printable form
- delete() - deletes the object (use with care, especially if you passed the ownership to C++)
- connect(signal, function) - connect the signal of the given object to a python function
- connect(signal, qobject, slot) - connect the signal of the given object to a slot of another QObject
- disconnect(signal, function) - disconnect the signal of the given object from a python function
- disconnect(signal, qobject, slot) - disconnect the signal of the given object from a slot of another QObject
- children() - returns the children of the object
- setParent(QObject) - set the parent
- QObject* parent() - get the parent
The below example shows how to connect signals in Python:
You can create dedicated wrapper QObjects for any C++ class. This is done by deriving from PythonQtCppWrapperFactory and adding your factory via addWrapperFactory(). Whenever PythonQt encounters a CPP pointer (e.g. on a slot or signal) and it does not known it as a QObject derived class, it will create a generic CPP wrapper. So even unknown C++ objects can be passed through Python. If the wrapper factory supports the CPP class, a QObject wrapper will be created for each instance that enters Python. An alternative to a complete wrapper via the wrapper factory are decorators, see Decorator slots
For each known C++ class, PythonQt provides a Python class. These classes are visible inside of the 'PythonQt' python module or in subpackages if a package is given when the class is registered.
A Meta class supports:
- access to all declared enum values
- constructors
- static methods
- unbound non-static methods
- help() and className()
From within Python, you can import the module 'PythonQt' to access these classes and the Qt namespace.
PythonQt introduces a new generic approach to extend any wrapped QObject or CPP object with
- constructors
- destructors (for CPP objects)
- additional slots
- static slots (callable on both the Meta object and the instances)
The idea behind decorators is that we wanted to make it as easy as possible to extend wrapped objects. Since we already have an implementation for invoking any Qt Slot from Python, it looked promising to use this approach for the extension of wrapped objects as well. This avoids that the PythonQt user needs to care about how Python arguments are mapped from/to Qt when he wants to create static methods, constructors and additional member functions.
The basic idea about decorators is to create a QObject derived class that implements slots which take one of the above roles (e.g. constructor, destructor etc.) via a naming convention. These slots are then assigned to other classes via the naming convention.
- SomeClassName* new_SomeClassName(...) - defines a constructor for 'SomeClassName' that returns a new object of type SomeClassName (where SomeClassName can be any CPP class, not just QObject classes)
- void delete_SomeClassName(SomeClassName* o) - defines a destructor, which should delete the passed in object o
- anything static_SomeClassName_someMethodName(...) - defines a static method that is callable on instances and the meta class
- anything someMethodName(SomeClassName* o, ...) - defines a slot that will be available on SomeClassName instances (and derived instances). When such a slot is called the first argument is the pointer to the instance and the rest of the arguments can be used to make a call on the instance.
The below example shows all kinds of decorators in action:
After you have registered an instance of the above ExampleDecorator, you can do the following from Python (all these calls are mapped to the above decorator slots):
Signal Slot Python Qt Python
Signal Slot Python Qt Download
Python Qt5 Signal Slot
In PythonQt, each wrapped C++ object is either owned by Python or C++. When an object is created via a Python constructor, it is owned by Python by default. When an object is returned from a C++ API (e.g. a slot), it is owned by C++ by default. Since the Qt API contains various APIs that pass the ownership from/to other C++ objects, PythonQt needs to keep track of such API calls. This is archieved by annotating arguments and return values in wrapper slots with magic templates:
These annotation templates work for since C++ pointer types. In addition to that, they work for QList<AnyObject*>, to pass the ownership for each object in the list.
Examples: