Python Qt Slot Decorator

  

A python method may be defined as a new qt slot by using the pyqtslot decorator. A new qt property may be defined using the pyqtproperty function. Note that the ability to define new qt signals, slots and properties from python is potentially useful to plugins conforming to any plugin interface and not just that used by designer. Very simple and intuitive. For more information on Python decorators, you might want to checkout the article - Python Decorators Overview to familiarise yourself. Finally, let's instantiate a Circle, hook up the signals to the slots, and move and resize it.

Python Qt Slot Decorator

Python Qt Slot Decorator Tool

And now we will go deeper into the work with Qt using PyQt5, taking advantage of modern Qt features. By such possibilities I mean QtQuick and QML. PyQt5 allows you to use Qt classes that can process QML code, and therefore you can write an interface to QML, and also send signals to the QML layer and invoke slots of objects inherited from QObject from the QML layer.

Python Qt Slot Decorator Set

To get meet with such possibilities of PyQt5, we will write a program that implements the following tasks:

  • The program interface should be written in QML
  • A class inherited from QObject and written in python must be implemented, with which we will interact from QML
  • An application using this class will need to add and subtract integers

Appearance of the application should look like this:

Project structure

There will be only two files in the project:

  • __main__.py - File application in python, there will also be a class for calculations
  • main.qml - Interface file on QML

Signals in PyQt5

The signal signature in the general case will look like this:

PyQt5.QtCore.pyqtSignal ( types [, name [, revision=0 [, arguments=[] ]]])

Create one or more overloaded unbound signals as a class attribute.

Parameters:
  • types – the types that define the C++ signature of the signal. Each type may be a Python type object or a string that is the name of a C++ type. Alternatively each may be a sequence of type arguments. In this case each sequence defines the signature of a different signal overload. The first overload will be the default.
  • name – the name of the signal. If it is omitted then the name of the class attribute is used. This may only be given as a keyword argument.
  • revision – the revision of the signal that is exported to QML. This may only be given as a keyword argument.
  • arguments – the sequence of the names of the signal’s arguments that is exported to QML. This may only be given as a keyword argument.

Slots in PyQt5

Python Qt Install

To define slots in PyQt5, a special decorator is used.

PyQt5.QtCore.pyqtSlot ( types [, name [, result [, revision=0 ]]])

Parameters:
  • types – the types that define the C++ signature of the slot. Each type may be a Python type object or a string that is the name of a C++ type.
  • name – the name of the slot that will be seen by C++. If omitted the name of the Python method being decorated will be used. This may only be given as a keyword argument.
  • revision – the revision of the slot that is exported to QML. This may only be given as a keyword argument.
  • result – the type of the result and may be a Python type object or a string that specifies a C++ type. This may only be given as a keyword argument.

__main__.py

main.qml