Development/Tutorials/Python introduction to signals and slots: Difference between revisions

From KDE TechBase
(start on python signal and slots tutorial)
 
(added som more code)
Line 5: Line 5:
The signal and slot architecture is design to simplify communication between objects.  It's a fact that GUI programming is mostly event driven.  The traditionally approach  to event driven programming is to use callbaks. Callbacks have a number of limitations that Qt tries to solve with it's signal and slot architecture. The concept is that every object can emit signals. For example when a button is clicked it emits a “clicked()” signal. Signals does not do anything alone, but when connected to a slot the code in the slot will be executed every time the signal is emitted. In python every function is a slot.  It's possible to connect one signal to multiple slots
The signal and slot architecture is design to simplify communication between objects.  It's a fact that GUI programming is mostly event driven.  The traditionally approach  to event driven programming is to use callbaks. Callbacks have a number of limitations that Qt tries to solve with it's signal and slot architecture. The concept is that every object can emit signals. For example when a button is clicked it emits a “clicked()” signal. Signals does not do anything alone, but when connected to a slot the code in the slot will be executed every time the signal is emitted. In python every function is a slot.  It's possible to connect one signal to multiple slots


==Prerequisites==
General understanding of the python programming language. No prior knowledge of QT is required.


==Connecting signals and slots.==
==Connecting signals and slots.==
Line 32: Line 34:


==Signals and slots with parameters==
==Signals and slots with parameters==
<code python>
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
def printNumber(number):
    print number
if __name__=="__main__":
    #First we create a QApplication and QPushButton
    app=QApplication(sys.argv)
    slider=QSlider(Qt.Horizontal)
   
   
    QObject.connect(slider,SIGNAL("valueChanged(int)"),printNumber)
   
   
    slider.show()
    #Start the evnt loop
    sys.exit(app.exec_())
</code>
==Creating custom signals==
==Signals and slots and python objects==
==Signals and slots and threading==
To send signal across threads we have to use the Qt.QueuedConnection parameter. Without this parameter the code will be executed in the same thread.
<code python>
import sys
from time import time
from PyQt4.QtCore import *
class A (QThread):
    def __init__(self):
        QThread.__init__(self)
   
    def afunc (self):
        p("starting in a()")
        self.emit(SIGNAL("asignal"))
        p("finished in a()")
     
    def bfunc(self):
        p("starting in b()")
        self.sleep(3)
        p("finished in b()")
               
    def run(self):
        self.exec_()
       
def p(msg): print str(int(time()-start)) + "s",msg
       
if __name__=="__main__":
    start=time()
    p("starting in __main__")
    app=QCoreApplication(sys.argv)
    a=A()
    a.start()
    QObject.connect(a,SIGNAL("asignal"),a.bfunc,Qt.QueuedConnection)
    a.afunc()
    p("finished in __main__")
     
    sys.exit(app.exec_())
</code>
<code>
Output:
0s starting in __main__
0s starting in a()
0s finished in a()
0s finished in __main__
0s starting in b()
3s finished in b()
without Qt.QueuedConnection the example will output:
0s starting in __main__
0s starting in a()
0s starting in b()
3s finished in b()
3s finished in a()
3s finished in __main__
</code>


[[Category:Python]]
[[Category:Python]]

Revision as of 22:59, 9 January 2007

Warning
This tutorial is work in progress.


Abstract

The signal and slot architecture is design to simplify communication between objects. It's a fact that GUI programming is mostly event driven. The traditionally approach to event driven programming is to use callbaks. Callbacks have a number of limitations that Qt tries to solve with it's signal and slot architecture. The concept is that every object can emit signals. For example when a button is clicked it emits a “clicked()” signal. Signals does not do anything alone, but when connected to a slot the code in the slot will be executed every time the signal is emitted. In python every function is a slot. It's possible to connect one signal to multiple slots

Prerequisites

General understanding of the python programming language. No prior knowledge of QT is required.

Connecting signals and slots.

This example demonstrates how to use the connect method to connect signals and slots.

from PyQt4.QtGui import * from PyQt4.QtCore import * import sys

if __name__=="__main__":

   #First we create a QApplication and QPushButton
   app=QApplication(sys.argv)
   exitButton=QPushButton("Exit")
   
   #Her we connect the exitButton's "clicked()" signals to the app's exit method. 
   #This will have the effect that every time someone clicks the exitButton the app.exit method will execute and the application will close.
   QObject.connect(exitButton,SIGNAL("clicked()"),app.exit)
   
   
   button.show()
   #Start the evnt loop
   sys.exit(app.exec_())


Signals and slots with parameters

from PyQt4.QtGui import * from PyQt4.QtCore import * import sys

def printNumber(number):

   print number

if __name__=="__main__":

   #First we create a QApplication and QPushButton
   app=QApplication(sys.argv)
   slider=QSlider(Qt.Horizontal)
   
   
   QObject.connect(slider,SIGNAL("valueChanged(int)"),printNumber)
   
   
   slider.show()
   #Start the evnt loop
   sys.exit(app.exec_())

Creating custom signals

Signals and slots and python objects

Signals and slots and threading

To send signal across threads we have to use the Qt.QueuedConnection parameter. Without this parameter the code will be executed in the same thread. import sys from time import time from PyQt4.QtCore import *

class A (QThread):

   def __init__(self):
       QThread.__init__(self)
   
   def afunc (self):
       p("starting in a()")
       self.emit(SIGNAL("asignal"))
       p("finished in a()")
      
   def bfunc(self):
       p("starting in b()")
       self.sleep(3) 
       p("finished in b()")
               
   def run(self):
       self.exec_()
       

def p(msg): print str(int(time()-start)) + "s",msg

if __name__=="__main__":

   start=time()
   p("starting in __main__")
   app=QCoreApplication(sys.argv)
   a=A()
   a.start()
   QObject.connect(a,SIGNAL("asignal"),a.bfunc,Qt.QueuedConnection)
   a.afunc()
   p("finished in __main__")
      
   sys.exit(app.exec_())

Output: 0s starting in __main__ 0s starting in a() 0s finished in a() 0s finished in __main__ 0s starting in b() 3s finished in b()


without Qt.QueuedConnection the example will output: 0s starting in __main__ 0s starting in a() 0s starting in b() 3s finished in b() 3s finished in a() 3s finished in __main__