xralf
xralf

Reputation: 3642

Why slots in PyQt

I'm studying Qt library. Recently I hit the concept of slots and so far I understand them as functions that are executed as reactions to certain events. Is it only this or something more?

Why did they choose such an exotic name slots for something such ordinary? This word has so many meanings. Which meaning did they intend to thought about slots in Qt?

thank you

Upvotes: 2

Views: 250

Answers (2)

ekhumoro
ekhumoro

Reputation: 120588

The terms signal and slot are somewhat mismatched.

A better choice might have been signal and receiver, or connector and slot.

In C++, a signal can only connect to a function that has been defined as a slot. Trying to connect a signal to an ordinary function would be like trying to connect a usb cable to an ethernet port.

However, in PyQt, this distinction is much less important, because any callable can be connected to a signal, not just predefined slots.

Having said that, more recent versions of PyQt also provide a pyqtSlot decorator which can be used to define a slot with several different signatures. And in general, in PyQt it is more efficient to connect to a predefined slot (whether C++ or PyQt) than to an ordinary python callable.

For full details of the signal and slot support in PyQt, see the reference guide.

Upvotes: 2

Mat
Mat

Reputation: 206689

Slots go with signals. You connect signals to slots. This is a core concept in Qt itself, it is not specific to PyQt.

The Qt documentation has the details: Signals & Slots.

It's just a very handy concept, makes for rather clean separation of layers in your code.

The slots themselves are plain functions (in C++), you can use them as normal functions as well as as "signal receivers".

Upvotes: 2

Related Questions