| 1 |
PSEP: 100 |
| 2 |
Title: New-style Signals and Slots |
| 3 |
Version: $Revision$ |
| 4 |
Last-Modified: $Date$ |
| 5 |
Author: Matti Airas <matti.p.airas@nokia.com> |
| 6 |
Status: Final |
| 7 |
Type: Standards Track |
| 8 |
Content-Type: text/x-rst |
| 9 |
Created: 30-Oct-2009 |
| 10 |
Post-History: 30-Oct-2009, 03-Nov-2009, 24-May-2010 |
| 11 |
|
| 12 |
|
| 13 |
Abstract |
| 14 |
======== |
| 15 |
|
| 16 |
This PSEP describes the support of PyQt's new-style signals and slots in |
| 17 |
PySide. Traditionally PyQt, and by extension PySide, has required the |
| 18 |
use of C++ function signature strings in connecting signals to slots. |
| 19 |
This has been widely regarded as unwieldy and non-Pythonic. A new-style |
| 20 |
signal and slot support was introduced in PyQt 4.5, in which the signals |
| 21 |
and slots may be manipulated using native Python objects and methods. |
| 22 |
This new syntax will be adopted to PySide with minor modifications. |
| 23 |
|
| 24 |
Rationale |
| 25 |
========= |
| 26 |
|
| 27 |
The PyQt's traditional syntax for signals and slots requires the use of |
| 28 |
explicit C++ style signal signatures [#old-style-signals]_. This has |
| 29 |
been regarded as unwieldy and non-Pythonic feature and an opportunity |
| 30 |
for improvement in the syntax. |
| 31 |
|
| 32 |
A new-style signal and slot syntax has been introduced in PyQt 4.5 |
| 33 |
[#new-style-signals]_. The new style provides a clean syntax to connect |
| 34 |
signals and slots using Python objects and methods instead of C++ style |
| 35 |
signature strings. An example of connecting and emitting signals using |
| 36 |
the new-style syntax is given below (adapted from [#new-style-signals]_):: |
| 37 |
|
| 38 |
class Foo(QtCore.QObject): |
| 39 |
|
| 40 |
# Define a new signal called 'trigger' that has no arguments. |
| 41 |
trigger = QtCore.pyqtSignal() |
| 42 |
|
| 43 |
def connect_and_emit_trigger(self): |
| 44 |
# Connect the trigger signal to a slot. |
| 45 |
self.trigger.connect(self.handle_trigger) |
| 46 |
|
| 47 |
# Emit the signal. |
| 48 |
self.trigger.emit() |
| 49 |
|
| 50 |
def handle_trigger(self): |
| 51 |
# Show that the slot has been called. |
| 52 |
|
| 53 |
print "trigger signal received" |
| 54 |
|
| 55 |
PyQt's new-style signal and slot mechanism provides a clean, concise, |
| 56 |
and Pythonic syntax for defining, connecting, disconnecting, and |
| 57 |
emitting signals. To maintain compatibility with PyQt, their new-style |
| 58 |
signal and slot mechanism will be adopted to PySide with minor |
| 59 |
modifications only. |
| 60 |
|
| 61 |
New Signal and Slot Syntax |
| 62 |
========================== |
| 63 |
|
| 64 |
PySide will adopt PyQt's new signal and slot syntax as-is. The PySide |
| 65 |
implementation will be functionally compatible with the PyQt 4.5 one, with |
| 66 |
the exceptions listed in the next Section. As PyQt compatibility will be |
| 67 |
the main guideline, the syntax specifics will not be described here. |
| 68 |
Instead, PyQt behaviour and documentation [#new-style-signals]_ shall be |
| 69 |
used as implementation guidelines. |
| 70 |
|
| 71 |
Differences from PyQt's syntax |
| 72 |
============================== |
| 73 |
|
| 74 |
PyQt's new signal and slot style utilizes method and decorator names |
| 75 |
specific to their implementation. These will be generalized according to |
| 76 |
the table below: |
| 77 |
|
| 78 |
======= ====================== ============= |
| 79 |
Module PyQt factory function PySide class |
| 80 |
======= ====================== ============= |
| 81 |
QtCore pyqtSignal Signal |
| 82 |
QtCore pyqtSlot Slot |
| 83 |
======= ====================== ============= |
| 84 |
|
| 85 |
In addition to the generalized class names, PyQt factory names will be |
| 86 |
provided as a compatibility feature. They will print out a deprecation |
| 87 |
warning, however. |
| 88 |
|
| 89 |
On the mailing list discussion, concerns were raised about the proposed |
| 90 |
naming scheme. One issue was the risk of Qt implementing similarly named |
| 91 |
C++ classes, resulting in name clashes in PySide. However, the risk may |
| 92 |
be regarded minor during the lifespan of Qt version 4, especially now |
| 93 |
that the signals and slots are classes and are written in the |
| 94 |
capitalized form. Even in the case this would happen, the issue could be |
| 95 |
circumvented in PySide by renaming the new C++ classes suitably. |
| 96 |
|
| 97 |
Another issue raised was the similarity of ``Signal`` and ``SIGNAL`` (as |
| 98 |
well as ``Slot`` and ``SLOT``) class and function names in QtCore. While |
| 99 |
the similarity is undeniably an issue, it could still be argued that the |
| 100 |
case difference is substantial enough to minimize any risk of confusion |
| 101 |
in practice. It could be argued that the non-Pythonic all-caps spelling |
| 102 |
refers to the old-style signals and slots, while the standard spelling |
| 103 |
refers to the Pythonic new-style signals. |
| 104 |
|
| 105 |
Compatibility |
| 106 |
============= |
| 107 |
|
| 108 |
Since the new signal and slot syntax does not break compatibility with |
| 109 |
the previous syntax, it will be implemented both as part of the original |
| 110 |
and the possible future PySide API. |
| 111 |
|
| 112 |
|
| 113 |
References |
| 114 |
========== |
| 115 |
|
| 116 |
.. [#old-style-signals] PyQt v4 Reference Manual, Section Old-style |
| 117 |
Signal and Slot Support |
| 118 |
(http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#old-style-signal-and-slot-support) |
| 119 |
|
| 120 |
.. [#new-style-signals] PyQt v4 Reference Manual, Section New-style |
| 121 |
Signal and Slot Support |
| 122 |
(http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#new-style-signal-and-slot-support) |
| 123 |
|
| 124 |
Contributors |
| 125 |
============ |
| 126 |
|
| 127 |
Hugo Lima, Mark Summerfield, Ville M. Vainio, Paul A. Giannaros, and |
| 128 |
Douglas Soares de Andrade have contributed in the initial discussion of |
| 129 |
this PSEP. |
| 130 |
|
| 131 |
After having the PSEP already accepted, Hugo Lima pointed out a naming |
| 132 |
scheme inconsistency, which caused the PSEP to be redrawn to the draft |
| 133 |
status to have the classes properly capitalized. |
| 134 |
|
| 135 |
Copyright |
| 136 |
========= |
| 137 |
|
| 138 |
This document has been placed in the public domain. |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
.. |
| 143 |
Local Variables: |
| 144 |
mode: indented-text |
| 145 |
indent-tabs-mode: nil |
| 146 |
sentence-end-double-space: t |
| 147 |
fill-column: 70 |
| 148 |
coding: utf-8 |
| 149 |
End: |