Whats the difference between a normal function and a slot in Qt? I have read that slots are like normal functions but in addition can be connected to signals, but normal functions can just as well.
Asked
Active
Viewed 1,402 times
2
-
What is a "normal function"? Where did you read that? – underscore_d May 17 '18 at 13:41
-
Possible duplicate of [Member function as a Qt Slot](https://stackoverflow.com/questions/13805271/member-function-as-a-qt-slot) – underscore_d May 17 '18 at 13:42
-
With the old syntax with macros `connect(a, SIGNAL(), b, SLOT())`, you had to declare methods as `slots`. With the new syntax `connect(a, &A::signal, b, &B::slot)`, you don't need it anymore. – Jaa-c May 17 '18 at 13:42
1 Answers
2
The difference is that slots with get MOC treatment and generate meta data, which is required for certain Qt functionality that involves runtime lookup by member names passed as strings.
For connections alone, plain member functions are perfectly OK since Qt 5 using the new connection syntax.
For Qt 4 you have no option but to use slots.

dtech
- 47,916
- 17
- 112
- 190
-
Also, slots are still needed if you want to interact between QML and C++ – Felix May 17 '18 at 17:22
-
1@Felix or `Q_INVOKABLE `, however I do consider QML to fall in the "lookup by name string" case, since that's how it works. – dtech May 17 '18 at 20:26
-
Is there any side effect if a slot function gets called just like a normal function (guess not but just to be sure!) ?? – Dumbo Feb 19 '19 at 09:43
-
1@SaeidYazdani not if you call it directly. Then it is just a function, what makes it a slot is the additional generated meta info, which you don't really use. When the slot is called by a signal it will execute it will execute in the thread in which the object lives, unless you explicitly specify a direct connection. – dtech Feb 19 '19 at 10:55