reference:http://zetcode.com/gui/pyqt4/eventsandsignals/
Events and Signals in PYQT4
In this part of the PYQT4 programming tutorial, we'll explore events and signals occurring in applications.
Events
All GUI applications is event-driven. Events is generated mainly by the user of an application. But they can is generated by other means as well:e.g. An Internet connection, a window manager, or a timer. When we call the application ' s exec_()
method, the application enters the main loop. The main loop fetches events and sends them to the objects.
In the event model, there is three participants:
- Event Source
- Event Object
- Event target
The event source is the object whose state changes. It generates events. The Event object encapsulates the state changes in the event source. The event target is the object, wants to be notified. Event source object Delegates the task of handling an event to the event target.
PYQT4 has a unique signal and slots mechanism to deal with events. Signals and slots is used for communication between objects. A signal is emitted when a particular event occurs. A Slot can is any Python callable. A slot is called when a signal connected to it is emitted.
New API
PyQt4.5 introduced a new style API for working with signals and slots.
QtCore.QObject.connect (Button, Qtcore.signal (' clicked () '), self.onclicked)
This is the old style API.
Button.clicked.connect (self.onclicked)
The new style adheres more to the Python standards.
Signals & Slots
This was a simple example demonstrating signals and slots in PYQT4.
#!/usr/bin/python#-*-coding:utf-8-*-"" "Zetcode PyQt4 Tutorial In this example, we connect a Signalof A qtgui.qslider to a slot of a qtgui.qlcdnumber. Author:jan Bodnarwebsite:zetcode.com last Edited:october "" "Import sysfrom PyQt4 import Qtgui, Qtcoreclass Example ( Qtgui.qwidget): def __init__ (self): Super (Example, self). __init__ () Self.initui () def initui (self): LCD = Qtgui.qlcdnumber (self) SLD = Qtgui.qslider (QtCore.Qt.Horizontal, self) VBox = Qtgui.qvboxlayout () vbox.addwidget (LCD) vbox.addwidget (SLD) self.setlayout (VBox) SLD. Valuechanged.connect (Lcd.display) self.setgeometry () self.setwindowtitle (' Signal & Amp Slot ') Self.show () def main (): App = Qtgui.qapplication (sys.argv) ex = Example () sys.exit (APP.E XEC_ ()) If __name__ = = ' __main__ ': Main ()
In our example, we display a and QtGui.QLCDNumber
a QtGui.QSlider
. We change the number by lcd
dragging the slider knob.
Sld.valueChanged.connect (Lcd.display)
Here we connect a signal of the slider to the slot of the number valueChanged
display
lcd
.
The sender is an object, sends a signal. The receiver is the object that receives the signal. Theslot is the method, the reacts to the signal.
Figure:signal & Slots
Reimplementing event Handler
Events in PYQT4 is processed often by reimplementing event handlers.
#!/usr/bin/python#-*-coding:utf-8-*-"" "Zetcode PyQt4 Tutorial In this example, we reimplement an event handler. Author:jan Bodnarwebsite:zetcode.com last Edited:october "" "Import sysfrom PyQt4 import Qtgui, Qtcoreclass Example ( Qtgui.qwidget): def __init__ (self): super ("Example, Self"). __init__ () Self.initui () def initui (self ): self.setwindowtitle (' Event handler ') self.show () def (self.setgeometry) Keypressevent (Self, e): if e.key () = = QtCore.Qt.Key_Escape: self.close () def main (): app = Qtgui.qapplication (SYS.ARGV) ex = Example () sys.exit (APP.EXEC_ ()) If __name__ = = ' __main__ ': Main ()
In our example, we reimplement the keyPressEvent()
event handler.
def keypressevent (self, e): if e.key () = = QtCore.Qt.Key_Escape: self.close ()
If We click the Escape button, the application terminates.
Event Sender
Sometimes it is convenient to know which widgets is the sender of a signal. For this, PYQT4 has the sender()
method.
#!/usr/bin/python#-*-coding:utf-8-*-"" "Zetcode PyQt4 Tutorial In this example, we determine the event Senderobject.aut Hor:jan Bodnarwebsite:zetcode.com last Edited:october "" "Import sysfrom PyQt4 import Qtgui, Qtcoreclass Example (QTG Ui. Qmainwindow): def __init__ (self): Super (Example, self). __init__ () Self.initui () de F Initui (self): BTN1 = Qtgui.qpushbutton ("button 1", self) btn1.move (+) btn2 = Qtgui.qpush Button ("button 2", self) Btn2.move (btn1.clicked.connect) (self.buttonclicked) Btn2.clicked.connect (self.buttonclicked) Self.statusbar () Self.setgeometry (300, 300, 290, Self.setwindowtitle (' Event sender ') self.show () def buttonclicked (self): Sender = Self.sender () Self.statusbar (). ShowMessage (Sender.text () + ' was pressed ') def main (): app = Qtgui . Qapplication (Sys.argV) ex = Example () sys.exit (APP.EXEC_ ()) If __name__ = = ' __main__ ': Main ()
We have both buttons in our example. In the buttonClicked()
method we determine which button we had clicked by calling the sender()
method.
Btn1.clicked.connect (self.buttonclicked) btn2.clicked.connect (self.buttonclicked)
Both buttons is connected to the same slot.
def buttonclicked (self): sender = Self.sender () Self.statusbar (). ShowMessage (Sender.text () + ' is pressed ')
We determine the signal source by calling the sender()
method. In the statusbar of the application, we show the label of the button being pressed.
Figure:event Sender
Emitting signals
Objects created from a QtCore.QObject
can emit signals. In the following example we'll see how we can emit custom signals.
#!/usr/bin/python#-*-coding:utf-8-*-"" "Zetcode PyQt4 Tutorial In this example, we show how to emit a Signal. Author:jan Bodnarwebsite:zetcode.com last Edited:january "" "Import sysfrom PyQt4 import Qtgui, Qtcoreclass communic Ate (qtcore.qobject): Closeapp = qtcore.pyqtsignal () class Example (Qtgui.qmainwindow): def __init__ (self) : Super (Example, self). __init__ () Self.initui () def initui (self): SE LF.C = Communicate () Self.c.closeapp.connect (self.close) self.setgeometry (300, 300, 290, 150) Self.setwindowtitle (' Emit signal ') self.show () def mousepressevent (Self, event): Self.c.closeapp.emit () def main (): App = Qtgui.qapplication (sys.argv) ex = Example () s Ys.exit (APP.EXEC_ ()) If __name__ = = ' __main__ ': Main ()
WE create a new signal called closeApp
. This signal is emitted during a mouse press event. The signal is connected to the close()
slot of the QtGui.QMainWindow
.
Class Communicate (Qtcore.qobject): Closeapp = qtcore.pyqtsignal ()
A signal is created with the as QtCore.pyqtSignal()
a class attribute of the external Communicate
class.
The custom closeApp
signal is connected to the close()
slot of the QtGui.QMainWindow
.
def mousepressevent (self, event): Self.c.closeapp.emit ()
When we click on the window with a mouse pointer, the closeApp
signal is emitted. The application terminates.
In this part of the PYQT4 tutorial, we have covered signals and slots.
[Repost] Events and Signals in PYQT4