Let’s say I have this signal:
signals:
void progressNotification(int progress);
I only recently learned about the emit keyword in Qt. Until now, I used to execute signals by just calling them like a regular function. So instead of:
emit progressNotification(1000 * seconds);
I would write:
progressNotification(1000 * seconds);
Calling them like that seemed to work, and all the connected slots would execute, so does using the emit keyword cause a different behaviour, or is it just syntactic sugar?
emitis just syntactic sugar. If you look at the pre-processed output of function that emits a signal, you’ll seeemitis just gone.The “magic” happens in the generated code for the signal emitting function, which you can look at by inspecting the C++ code generated by moc.
For example a
foosignal with no parameters generates this member function:And the code
emit foo();is pre-processed to simplyfoo();emitis defined inQt/qobjectdefs.h(in the open-source flavor of the source anyway), like this:(The define guard is to allow you to use Qt with other frameworks that have colliding names via the
no_keywordsQMake config option.)