Events and signals that you must learn every day in PyQt5
Apr 20, 2018 pm 01:59 PMThis article mainly introduces in detail the relevant information about events and signals that must be learned every day in PyQt5. It has a certain reference value. Interested friends can refer to it.
We will explore this part How PyQt5 events and signals are implemented in applications.
EventsEvents
All GUI applications are event-driven. Application events are primarily generated by the user, but they can also be generated by other methods, such as 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 detects various events and sends them to event objects.
In the event model, there are three participants:
event source (event source)
event object (event Object)
event target (event target)
The event source is the state change of the object that generates events. An event object (event) is an object that encapsulates state changes in an event source. The event target is the object that wishes to be notified. The event source object represents the task of processing an event to the event target.
PyQt5 uses a unique signal and slot mechanism to handle events. Signals and slots are used for communication between objects. When a specific event occurs, a signal is emitted. The slot can be any Python call. The signal is emitted when the slot connected to it is called.
Signals & slotsSignals and slots
This is a simple example demonstrating PyQt5's signals and slots.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
In our example, QtGui.QLCDNumber and QtGui.QSlider will be used. We change the LCD numbers by dragging the slider.
1 |
|
Here, the valueChanged signal of the slider is connected to the display slot of the lcd.
A transmitter is an object that sends signals. A receiver is an object that receives a signal. The slot is the method of feedback to the signal.
After the program is executed
Override the system event handler
Events are often processed in PyQt5 through Override the event handler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
In our case, we reimplement the keyPressEvent() event handler.
1 2 3 |
|
If we press the Esc key on the keyboard, the application terminates.
Event senderEvent sending
In order to easily distinguish multiple event sources connected to the same event target, the sender() method can be used in PyQt5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
In our example there are two buttons. Both buttons are connected to the buttonClicked() method and we respond to the clicked button by calling the sender() method.
1 2 |
|
Two buttons are connected to the same slot.
1 2 3 4 |
|
We determine the signal source by calling the sender() method. In the application's status bar, displays the label of the pressed button.
After the program is executed
Customized emission signal
Signals can be emitted from an object created by a QObject. In the following example we will look at how we can customize the emitted signal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
We create a new signal called closeApp. This signal emits a mouse press event. This signal is connected to the close() slot in QMainWindow.
1 2 |
|
Create a Communicate class inherited from QObject, which has a property of the pyqtSignal() class.
1 2 |
|
Connect our custom closeApp signal to the close() slot in QMainWindow.
1 2 |
|
When our mouse clicks on the program window, the closeApp signal is emitted (emit). Application terminated.
Related recommendations:
Python PyQt4 implements QQ drawer effect
PyQt implements interface flip switching effect
The above is the detailed content of Events and signals that you must learn every day in PyQt5. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What is an analog signal and what is a digital signal

Get upcoming calendar events on your iPhone lock screen

Super complete! Python graphical interface framework PyQt5 usage guide!

Solution to the problem that the mobile phone cannot connect to WiFi (how to solve the problem that the mobile phone has a WiFi signal but cannot access the Internet)

In JavaScript, what is the purpose of the 'oninput' event?

How to implement calendar functions and event reminders in PHP projects?
