Using emit vs calling a signal as if it's a regular function in Qt

c++, qt

Solution

`emit` is just syntactic sugar. If you look at the pre-processed output of function that emits a signal, you'll see `emit` is 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 `foo` signal with no parameters generates this member function:

void W::foo()
{
    QMetaObject::activate(this, &staticMetaObject, 0, 0);
}

And the code `emit foo();` is pre-processed to simply `foo();`

`emit` is defined in `Qt/qobjectdefs.h` (in the open-source flavor of the source anyway), like this:

#ifndef QT_NO_EMIT
# define emit
#endif

(The define guard is to allow you to use Qt with other frameworks that have colliding names via the `no_keywords` QMake config option.)

Problem

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?

Original source