Home / Guides / Core

PyQt Signals and Slots Explained (With Examples)

Understand PyQt signals and slots — how widgets emit events, how to connect them to functions, passing data, and making your own custom signals.

CORE

PyQt Signals and Slots Explained (With Examples)

Signals and slots are how PyQt apps respond to the user. Once this idea clicks, every interactive feature becomes easy. This guide explains it with clear, short examples you can run.

The core idea

A signal is emitted when something happens. A button is clicked. Text changes. A timer fires. A slot is simply a function that runs in response. You connect a signal to a slot, and Qt handles the rest.

That is the whole model. Widgets announce events. Your functions listen. It keeps your code loose and tidy, because the button does not need to know what your function does.

Connecting a signal

Here is the pattern you will use most:

connect.py
button = QPushButton("Save")

def on_save():
    print("Saved!")

button.clicked.connect(on_save)

Every click now calls on_save. Notice you pass the function without the parentheses. You are handing over the function itself, not calling it.

Signals that carry data

Some signals pass a value along. For example, QLineEdit.textChanged sends the new text each time it changes:

data.py
edit = QLineEdit()
edit.textChanged.connect(lambda text: print("Now:", text))

Your slot simply accepts the value as an argument. This is how live search boxes and character counters work.

One to many, many to one

Signals are flexible. You can connect one signal to several slots, so a single click updates two places at once. You can also connect several signals to one slot, so different buttons share the same handler.

This flexibility is what lets large apps stay organised. Each piece does its job without tangling into the others.

Making custom signals

You can define your own signals to decouple parts of your app. This is great for background work that needs to report back:

custom.py
from PyQt6.QtCore import QObject, pyqtSignal

class Worker(QObject):
    done = pyqtSignal(str)
    def run(self):
        self.done.emit("finished")

w = Worker()
w.done.connect(lambda msg: print(msg))
w.run()

The worker does not know who is listening. It just emits done. Anyone can connect to it. That is clean design.

Common pitfalls

Two slips catch beginners. First, connect to the function name, not a call — write connect(on_save), not connect(on_save()). Second, when you connect inside a loop, capture the loop variable with a default argument, or every connection will share the last value.

Disconnecting signals

Usually you connect a signal once and leave it. Now and then you need to stop listening, for example when a screen closes. You can disconnect a specific slot, or disconnect everything from a signal in one call.

Disconnecting matters most for objects that come and go. If you forget, a slot might run against a widget that no longer exists. For long-lived windows it rarely comes up, but it is good to know the option is there.

Signals across threads

Heavy work should not run on the main thread, or the window freezes. The common pattern is to do the work in a background worker and report progress back with signals. Qt delivers those signals safely to the main thread for you.

This is why custom signals are so useful. A worker emits progress and done, and the window updates a bar and a label in response, all without risky shared state.

Designing with signals

Signals encourage a clean style. A widget announces that something happened and does not care who listens. Other parts of the app subscribe to what they need. This keeps pieces loosely joined and easy to change.

When you plan a feature, ask what events it produces and what should react to them. Thinking in signals and slots often makes the whole design fall into place.

Why this design is powerful

Signals and slots solve a hard problem elegantly: how do parts of an app talk without becoming tangled? The answer is that a widget announces events and does not care who, if anyone, is listening. Other parts subscribe to what they need.

This loose coupling keeps large apps maintainable. You can add a new reaction to a button click without touching the button. You can remove a feature without hunting through unrelated code. Each piece minds its own business.

It is the same idea behind event systems in many modern frameworks. Learning it in PyQt gives you a mental tool you will reuse far beyond Qt.

Everyday patterns

A handful of patterns cover most real needs. Connect a button to an action method. Connect a text box's change signal to a live preview. Connect a slider to a label so the number updates as it moves.

For longer tasks, run the work in a background worker and connect its progress signal to a progress bar. This keeps the window responsive while the job runs, which users always notice and appreciate.

Once these patterns are familiar, building interactive features becomes routine. You stop thinking about the plumbing and start thinking about what the app should do.

Key takeaways

Signals and slots are the heart of an interactive PyQt app. A widget emits a signal when something happens; a slot is a function that responds; you connect them with a single line. That simple model powers every button, box and slider.

The design keeps apps tidy. A widget announces events without caring who listens, so parts stay loosely joined and easy to change. You can add or remove reactions without touching the widget that triggers them.

Watch two common slips: connect to a function name without calling it, and capture loop variables when connecting inside a loop. Avoid those and define your own signals when you need them, and interactive features become routine.

FAQs

What exactly is a slot?

Any Python function or method. You do not need special decorators for most cases.

Can a signal trigger more than one function?

Yes. Connect it as many times as you like. All connected slots run.

How do I send data with a signal?

Declare it with a type, like pyqtSignal(str), then pass the value to emit.

Why does my slot run immediately?

You added parentheses and called it by mistake. Remove them so you pass the function, not its result.

Conclusion

Signals and slots are the beating heart of PyQt. Widgets emit events, your functions respond, and you wire them together with one line. Master this and interactive apps become simple. Put it to work in our first example, and learn the widgets that emit these signals in our widgets guide.

Ready to start? Download PyQt or run pip install PyQt6.