Trevor Boyd Smith
Trevor Boyd Smith

Reputation: 19253

Qt question: How do signals and slots work?

How do signals and slots work at a high level abstraction?

How are signals and slots implemented at a high level abstraction?

Upvotes: 15

Views: 21342

Answers (4)

Neil
Neil

Reputation: 25825

I've actually read this Qt page about it, and it does a good job of explaining:

https://doc.qt.io/qt-5/signalsandslots.html

Upvotes: 15

CreativeMind
CreativeMind

Reputation: 917

This is really very nice explanation.

http://woboq.com/blog/how-qt-signals-slots-work.html

Upvotes: 4

Thomi
Thomi

Reputation: 11808

As other people said, there's very good Qt documetnation available for this topic. If you want to know what happens under the hood, this info might help you:

Slots are just regular methods. Nothing special there, EXCEPT moc will save their signature in a table in the intermediate .moc file - you can see this table quite clearly when you look through this file.

This table allows you to call a method using it's signature. The SLOT(mySlot(int)) macro boils down to a string representation of the method in question. There are several ways you can do this, see the documentation for QMetaObject for example.

When a you connect a signal to a slot, the signal and slot signatures are stored for later use. When a signal is emitted, all the slots previously connected to that signal are called using the method described above.

If you want to know more, I suggest looking through the moc-generated code, and stepping through a signal emission and the internals of the connect() call. There's no magic here, but there is a lot of cleverness.

Upvotes: 11

Ron Warholic
Ron Warholic

Reputation: 10074

We somewhat answered it in your other question

Why does Qt use its own make tool, qmake?

But to go into somewhat more detail, the MOC parses your file looking for signal/slot declarations (as well as properties and the other supported constructs) and generates intermediate code files based on those. These intermediate code files provide strongly-typed access to the signals and slots for the library to use to communicate with your objects.

qmake generates a makefile that automatically includes these intermediate files (as well as any UI or resource files generated) as well as your own code so you can build with your tool chain of choice.

Upvotes: 5

Related Questions