signal and slot in pyside
Signals and Slots in PySide
This page describes the use of signals and slots in PySide. 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.
PyQt’s new-style signals and slots were introduced in PyQt v4.5. The main goal of this new-style is to provide a more Pythonic syntax to Python programmers. PySide uses PSEP 100 [pyside.org] as its implementation guideline.
QtGui.QPushButton("Call someFunc")
QtGui.QPushButton("Call someFunc")
QtCore, QtGui
- 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.
- #!/usr/bin/env python
- import sys
- from PySide import QtCore
- # define a new slot that receives a string and has
- # 'saySomeWords' as its name
- @QtCore.Slot(str)
- def saySomeWords(words):
- print words
- class Communicate(QtCore.QObject):
- # create a new signal on the fly and name it 'speak'
- speak = QtCore.Signal(str)
- someone = Communicate()
- # connect signal and slot
- someone.speak.connect(saySomeWords)
- # emit 'speak' signal
- someone.speak.emit("Hello everybody!")
- Add some overloads. A small modification of the previous example, now with overloaded decorators.
- #!/usr/bin/env python
- import sys
- from PySide import QtCore
- # define a new slot that receives a C 'int' or a 'str'
- # and has 'saySomething' as its name
- @QtCore.Slot(int)
- @QtCore.Slot(str)
- def saySomething(stuff):
- print stuff
- class Communicate(QtCore.QObject):
- # create two new signals on the fly: one will handle
- # int type, the other will handle strings
- speakNumber = QtCore.Signal(int)
- speakWord = QtCore.Signal(str)
- someone = Communicate()
- # connect signal and slot properly
- someone.speakNumber.connect(saySomething)
- someone.speakWord.connect(saySomething)
- # emit each 'speak' signal
- someone.speakNumber.emit(10)
- someone.speakWord.emit("Hello everybody!")
- An example with slot overloads and more complicated signal connections and emissions:
- #!/usr/bin/env python
- import sys
- from PySide import QtCore
- # define a new slot that receives an C 'int' or a 'str'
- # and has 'saySomething' as its name
- @QtCore.Slot(int)
- @QtCore.Slot(str)
- def saySomething(stuff):
- print stuff
- class Communicate(QtCore.QObject):
- # create two new signals on the fly: one will handle
- # int type, the other will handle strings
- speak = QtCore.Signal((int,), (str,))
- someone = Communicate()
- # connect signal and slot. As 'int' is the default
- # we have to specify the str when connecting the
- # second signal
- someone.speak.connect(saySomething)
- someone.speak[str].connect(saySomething)
- # emit 'speak' signal with different arguments.
- # we have to specify the str as int is the default
- someone.speak.emit(10)
- someone.speak[str].emit("Hello everybody!")
- An example of an object method emitting a signal:
- #!/usr/bin/env python
- import sys
- from PySide import QtCore
- class Communicate(QtCore.QObject): # !!! Must inherit QObject for signals
- speak = QtCore.Signal()
- def __init__(self):
- # !!! Must init QObject else runtime error: PySide.QtCore.Signal object has no attribute ‘emit’
- super(Communicate, self).__init__()
- def speakingMethod():
- self.speak.emit()
- someone = Communicate()
- someone.speakingMethod()
- Signals are runtime objects owned by instances, they are not class attributes:
- Communicate.speak.connect(saySomething) # Erroneous: refers to class Communicate, not an instance of the class
- # raises exception: AttributeError: 'PySide.QtCore.Signal' object has no attribute 'connect'
QtCore import Signal as pyqtSignal
or
- QtCore.pyqtSignal = QtCore.Signal
- QtCore.pyqtSlot = QtCore.Slot
This way any call to pyqtSignal or pyqtSlot will be translated to a Signal or Slot call.