📜 ⬆️ ⬇️

PyQt4 - Signals and Events

Developments
Events are an important part of GUI programming. Events are generated by users or by the system. When we call the exec () method, the application starts the main loop. It receives events and sends them to objects. Trolltech presents a unique signal and slot mechanism.

Signals and Slots
Signals are generated when the user clicks a button, marks a checkbox, etc. Signals can also be sent by the environment. For example, when the timer is triggered. A slot is a method that responds to a signal.
#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

class SigSlot(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)

self.setWindowTitle('signal & slot')

lcd = QtGui.QLCDNumber(self)
slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)

vbox = QtGui.QVBoxLayout()
vbox.addWidget(lcd)
vbox.addWidget(slider)

self.setLayout(vbox)
self.connect(slider, QtCore.SIGNAL('valueChanged(int)'),
lcd, QtCore.SLOT('display(int)') )

self.resize(250, 150)

app = QtGui.QApplication(sys.argv)
qb = SigSlot()
qb.show()
sys.exit(app.exec_())

In this case, we display the slider and LCD dial. The number on the dial is changed by dragging the slider.
self.connect(slider, QtCore.SIGNAL('valueChanged(int)'),
lcd, QtCore.SLOT('display(int)'))

Here we connect the valueChanged () slider signal to the display () slot of the clock face.
The connect () method has four parameters. Sender (sender) - the object that sends the signal. Signal (signal) - the generated signal. Receiver (receiver) - the object that receives the signal. Slot (slot) - a method that responds to a signal.
Signals and Slots

Change event handler
Events in PyQt4 are handled in often by modifying event handlers.
#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

class Escape(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)

self.setWindowTitle('escape')
self.resize(250, 150)
self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()') )

def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()

app = QtGui.QApplication(sys.argv)
qb = Escape()
qb.show()
sys.exit(app.exec_())

In our example, we are reinitializing the keyPressEvent () event handler.
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()

If we click on the Escape button, then close the application.

Signal generation
Objects created from QtCore.QObject can generate signals. If we click a button, the clicked () signal is generated. In the following example, we will show how to do this.
#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

class Emit(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)

self.setWindowTitle('emit')
self.resize(250, 150)
self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()') )

def mousePressEvent(self, event):
self.emit(QtCore.SIGNAL('closeEmitApp()'))

app = QtGui.QApplication(sys.argv)
qb = Emit()
qb.show()
sys.exit(app.exec_())

We create a new closeEmitApp () signal. This signal is generated when a mouse button is pressed.
def mousePressEvent(self, event):
self.emit(QtCore.SIGNAL('closeEmitApp()'))

The signal is created using the emit () method.
self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()'))

Here we connect the closeEmitApp () signal created by us to the close () slot.

')

Source: https://habr.com/ru/post/31690/


All Articles