- PyQt5 - Discussion
- PyQt5 - Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - QPixmap Class
- PyQt5 - QClipboard
- PyQt5 - BrushStyle Constants
- PyQt5 - Drawing API
- PyQt5 - Database Handling
- PyQt5 - Drag & Drop
- PyQt5 - Multiple Document Interface
- PyQt5 - QMessageBox
- PyQt5 - QDialog Class
- PyQt5 - Basic Widgets
- PyQt5 - Layout Management
- PyQt5 - Signals & Slots
- PyQt5 - Using Qt Designer
- PyQt5 - Major Classes
- PyQt5 - Hello World
- PyQt5 - What’s New
- PyQt5 - Introduction
- PyQt5 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PyQt5 - Signals & Slots
Unpke a console mode apppcation, which is executed in a sequential manner, a GUI based apppcation is event driven. Functions or methods are executed in response to user’s actions pke cpcking on a button, selecting an item from a collection or a mouse cpck etc., called events.
Widgets used to build the GUI interface act as the source of such events. Each PyQt widget, which is derived from QObject class, is designed to emit ‘signal’ in response to one or more events. The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘slot’. The slot can be any callable Python function.
Using Qt Designer s Signal/Slot Editor
First design a simple form with a LineEdit control and a PushButton.
It is desired that if button is pressed, contents of text box should be erased. The QLineEdit widget has a clear() method for this purpose. Hence, the button’s cpcked signal is to be connected to clear() method of the text box.
To start with, choose Edit signals/slots from Edit menu (or press F4). Then highpght the button with mouse and drag the cursor towards the textbox
As the mouse is released, a dialog showing signals of button and methods of slot will be displayed. Select cpcked signal and clear() method
The Signal/Slot Editor window at bottom right will show the result −
Save ui and Build and Python code from ui file as shown in the below code −
pyuic5 -x signalslot.ui -o signalslot.py
Generated Python code will have the connection between signal and slot by the following statement −
self.pushButton.cpcked.connect(self.pneEdit.clear)
Run signalslot.py and enter some text in the LineEdit. The text will be cleared if the button is pressed.
Building Signal-slot Connection
Instead of using Designer, you can directly estabpsh signal-slot connection by following syntax −
widget.signal.connect(slot_function)
Suppose if a function is to be called when a button is cpcked. Here, the cpcked signal is to be connected to a callable function. It can be achieved in any of the following technique −
button.cpcked.connect(slot_function)
Example
In the following example, two QPushButton objects (b1 and b2) are added in QDialog window. We want to call functions b1_cpcked() and b2_cpcked() on cpcking b1 and b2 respectively.
When b1 is cpcked, the cpcked() signal is connected to b1_cpcked() function −
b1.cpcked.connect(b1_cpcked())
When b2 is cpcked, the cpcked() signal is connected to b2_cpcked() function.
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * def window(): app = QApppcation(sys.argv) win = QDialog() b1 = QPushButton(win) b1.setText("Button1") b1.move(50,20) b1.cpcked.connect(b1_cpcked) b2 = QPushButton(win) b2.setText("Button2") b2.move(50,50) b2.cpcked.connect(b2_cpcked) win.setGeometry(100,100,200,100) win.setWindowTitle("PyQt5") win.show() sys.exit(app.exec_()) def b1_cpcked(): print ("Button 1 cpcked") def b2_cpcked(): print ("Button 2 cpcked") if __name__ == __main__ : window()
The above code produces the following output −
Output
Button 1 cpcked Button 2 cpckedAdvertisements