Qt5 Signal Slot Syntax

Qt5 Signal Slot Syntax Rating: 4,9/5 3172 reviews
Python

Qt 5.5 connect fails using new signals and slots syntax with Intel compiler. XML Word Printable. Testnewqt5connectsyntax.pro 0.0 kB 15 Sep '15. Slots and signals must have same parameters. Otherwise, the connection will not occur. Not only for connection, slot function must have same parameters with signal. For example, this sample doesn’t work: QObject::connect(ui.comboBox, SIGNAL (activated(int)), this, SLOT (onComboboxActivated)); But it works. The connection type is determined when the signal is emitted. Qt::DirectConnection: This slot is invoked immediately when the signal is emitted. The slot is executed in the signaling thread. Qt::QueuedConnection: The slot is invoked when control returns to the event loop of the receiver’s thread. The slot is executed in the receiver’s thread.

  • 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

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.

Qt5 Signal Slot Syntax Analyzer

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

Qt5 Signal Slot Syntax Python

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 −

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 −

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.

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

Qt5 Signal Slot Syntax Tool

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

The above code produces the following output −

Output