Qt5 Signal Slot Tutorial

Posted on  by admin

Due to which the statement, A signal-slot connection is removed when either of the Qt objects involved are destroyed” is incorrect in Qt 4.8 but is fixed in Qt 5.0. Ok shall retain the existing disconnect statements used in Qt 4.8.x and call deleteLater instead.

  1. Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt. In GUI programming, when we change one widget, we often want another widget to be notified. More generally, we want objects of any kind to be able to communicate with one another.
  2. Qt 5 Documentation — QMainWindow Signals As you can see, alongside the two QMainWindow signals, there are 4 signals inherited from QWidget and 2 signals inherited from Object. If you click through to the QWidget signal documentation you can see a.windowTitleChanged signal implemented here.
  3. Qt already provides signals and slots for its classes, which you can use in your application. For example, QPushButton has a signal clicked, which will be triggered when the user clicks on the button. The QApplication class has a slot quit function, which can be called when you want to terminate your application.
  • PyQt5 Tutorial
  • PyQt5 Useful Resources
  • Selected Reading

Unlike a console mode application, which is executed in a sequential manner, a GUI based application is event driven. Functions or methods are executed in response to user’s actions like clicking on a button, selecting an item from a collection or a mouse click 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

Qt Signal Slot Tutorial

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 clicked 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 highlight 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 clicked 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 −

Tutorial

Qt5 Signal Slot Example

Generated Python code will have the connection between signal and slot by the following statement −

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 establish signal-slot connection by following syntax −

Qt5 Signal Slot Tutorial For Beginners

Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function. It can be achieved in any of the following technique −

Example

In the following example, two QPushButton objects (b1 and b2) are added in QDialog window. We want to call functions b1_clicked() and b2_clicked() on clicking b1 and b2 respectively.

Qt5 Signal Slot Tutorial Arduino

When b1 is clicked, the clicked() signal is connected to b1_clicked() function −

Qt5 Signal Slot Tutorial Android Studio

When b2 is clicked, the clicked() signal is connected to b2_clicked() function.

The above code produces the following output −

Output