Pyqt4 Signals And Slots Example

An introduction to creating PySide/PyQt signals and slots, using QObject. How signals and slots are useful, and what they can do when developing in PySide/PyQt.

  1. Pyqt Examples And Demos
  2. Pyqt Signals And Slots Tutorial
  3. Pyqt5 Signals And Slots

This page describes the use of signals and slots in Qt for Python.The emphasis is on illustrating the use of so-called new-style signals and slots, although the traditional syntax is also given as a reference.

The main goal of this new-style is to provide a more Pythonic syntax to Python programmers.

Pyqt4 Signals And Slots Example
  • 2New syntax: Signal() and Slot()

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.It is necessary to inform the object, its signal (via macro) and a slot to be connected to.

New syntax: Signal() and Slot()

Pyqt4 Signals And Slots Example

The new-style uses a different syntax to create and to connect signals and slots.The previous example could be rewritten as:

Using QtCore.Signal()

Signals can be defined using the QtCore.Signal() class.Python types and C types can be passed as parameters to it.If you need to overload it just pass the types as tuples or lists.

In addition to that, it can receive also a named argument name that defines the signal name.If nothing is passed as name then the new signal will have the same name as the variable that it is being assigned to.

The Examples section below has a collection of examples on the use of QtCore.Signal().

Note: Signals should be defined only within classes inheriting from QObject.This way the signal information is added to the class QMetaObject structure.

Using QtCore.Slot()

Slots are assigned and overloaded using the decorator QtCore.Slot().Again, to define a signature just pass the types like the QtCore.Signal() class.Unlike the Signal() class, to overload a function, you don't pass every variation as tuple or list.Instead, you have to define a new decorator for every different signature.The examples section below will make it clearer.

Another difference is about its keywords.Slot() accepts a name and a result.The result keyword defines the type that will be returned and can be a C or Python type.name behaves the same way as in Signal().If nothing is passed as name then the new slot will have the same name as the function that is being decorated.

Examples

The examples below illustrate how to define and connect signals and slots in PySide2.Both basic connections and more complex examples are given.

  • Hello World example: the basic example, showing how to connect a signal to a slot without any parameters.
  • Next, some arguments are added. This is a modified Hello World version. Some arguments are added to the slot and a new signal is created.
  • Add some overloads. A small modification of the previous example, now with overloaded decorators.
  • An example with slot overloads and more complicated signal connections and emissions (note that when passing arguments to a signal you use '[]'):
  • An example of an object method emitting a signal:
  • An example of a signal emitted from another QThread:
  • Signals are runtime objects owned by instances, they are not class attributes:
Retrieved from 'https://wiki.qt.io/index.php?title=Qt_for_Python_Signals_and_Slots&oldid=35927'

These PyQt examples show you how to create a desktop app with Python and Qt. Start with 'Hello World' or browse the official PyQt demos. You can run every example yourself on Windows, Mac or Linux. All you need is Python 3. For instructions, please see below.

Hello World!Common PyQt WidgetsLayoutsSignals and SlotsQt Designer & Python
QML Python exampleQt Text EditorPackaging & deploymentQt Dark Theme
Action ShooterChat ClientTree ViewsLists
Custom TablesPyQt database example

These examples are taken from the following book:


Python and Qt: The Best Parts
by Michael Herrmann

Qt signals and slots tutorial

Official PyQt demos

The PyQt source archive also contains a large number of sample files. You can find them reproduced here in the src/pyqt-official directory. The easiest way to start them is to follow the instructions about running examples below, then execute the following commands:

This starts the PyQt example launcher:

You can use it to easily browse and run the official demo applications. The following examples are quite nice for instance:

  • Quick / Animation / ColorAnimation
  • Graphics Effects / Lighting and Shadows
  • Desktop / System Tray
  • Desktop / Screenshot
  • Widgets / Tetrix

Running the examples

Running the examples is really easy. The only thing you need is Python 3.

First, download the ZIP archive of this repository and unpack it. Open a command prompt and use cd to navigate into the top-level directory of the archive.

Create a virtual environment via the command:

This creates the folder venv/ in your current directory. It will contain the necessary libraries for running the examples.

To activate the virtual environment, use the following command:

Now execute the following to install the necessary dependencies:

Once you have done this, use cd to navigate to the example you're interested in in the src/ folder. For example:

You'll find a .py file there, typically main.py. You can run it with the command:

Please note that the virtual environment must still be active for this to work.

Using PySide2

This repository uses PyQt5 to use Qt from Python. Another, alternative binding is PySide2 (also called 'Qt for Python'). It is less mature than PyQt5 but has the advantage that you can use it for free in commercial projects.

If you want to use PySide2 instead of PyQt5, simply replace all mentions of the latter by the former. For instance, in src/requirements.txt, replace PyQt5 by PySide2. Similarly for any code examples: from PyQt5.QtWidgets ... becomes from PySide2.QtWidgets ... etc.

Pyqt Examples And Demos

Alternatively, if you don't want to commit to either of the two bindings at this stage, you can also use Qt.py. This is an abstraction over PySide2 and PyQt5. It loads whichever of the two bindings is available. To use it for the examples presented here, replace all mentions of PyQt5 by just Qt.

Licensing

Except where otherwise indicated, you may use the source code of examples 1 - 15 in the src/ directory under the terms of the MIT or GPLv3 licenses.

Pyqt Signals And Slots Tutorial

The official PyQt demos in src/pyqt-official are licensed under the GPL.

Pyqt5 Signals And Slots

The screenshots in this repository may be used under the terms of the CC BY-NC-SA 4.0 if you prominently mention and link to Michael Herrmann's PyQt5 book.

Comments are closed.