Pyqt Slots And Signals Example

  • PyQt Tutorial
Slot
  • PyQt Useful Resources
  • Selected Reading

PyQt supports many type of signals, not just clicks. Example We can create a method (slot) that is connected to a widget. A slot is any callable function or method. On running the application, we can click the button to execute the action (slot). One of the key features of Qt is its use of signals and slots to communicate between objects.For a simple but complete and fully documented example of a custom widget that defines new Qt signals, slots and properties, and its plugin, look in the examples/designer/plugins directory of the PyQt source package.May 10, 2014 Tutorial PyQt #9.

A rectangular box before the text label appears when a QCheckBox object is added to the parent window. Just as QRadioButton, it is also a selectable button. Its common use is in a scenario when the user is asked to choose one or more of the available options.

Unlike Radio buttons, check boxes are not mutually exclusive by default. In order to restrict the choice to one of the available items, the check boxes must be added to QButtonGroup.

The following table lists commonly used QCheckBox class methods −

Sr.No.Methods & Description
1

setChecked()

Changes the state of checkbox button

2

setText()

Sets the label associated with the button

3

text()

Retrieves the caption of the button

4

isChecked()

Checks if the button is selected

5

setTriState()

Provides no change state to checkbox

Each time a checkbox is either checked or cleared, the object emits stateChanged() signal.

Pyqt Signal Connect

Example

Here, two QCheckBox objects are added to a horizontal layout. Their stateChanged() signal is connected to btnstate() function. The source object of signal is passed to the function using lambda.

Pyqt Slots And Signals Tutorial

The isChecked() function is used to check if the button is checked or not.

The complete code is as follows −

As mentioned earlier, checkBox buttons can be made mutually exclusive by adding them in the QButtonGroup object.

Pyqt Example Code

QButtonGroup object, provides abstract container for buttons and doesn’t have a visual representation. It emits buttonCliked() signal and sends Button object’s reference to the slot function btngroup().

And

Pyqt5 Signal Slot Example

The btngroup() function displays the caption of the clicked checkbox.