Search code examples
c++qtqtcoreqt-signalsmoc

Difference between emit and emit()


In Qt, both of them are valid, and behave the same:

emit someSignal(value);

vs

emit(someSignal(value));

Is there any difference?


Solution

  • Is there any difference?

    There is no difference other than the external bracket being needless, so programmers will prefer that, and it is also more conventionally used in Qt projects out there.

    The reason for the no difference is due to this:

    # define emit
    

    You can see the definition of this in the source code.

    Therefore, it is basically just declared to an "empty" string and preprocessor will replace your variants to:

    someSignal(value);
    

    or

    (someSignal(value));
    

    You could see this yourself if, for a quick test, you stop the execution of the compiler after the preprocessor is run, e.g. the -E option in case of gcc.

    Further information: someone in the comment had doubts about the emit keyword because it pollutes the global macro namespace. That is true, but it is a good way of ensuring that there is some common way of doing this which is a good thing.

    On the other hand, this is not compulsory, so anyone could redefine, undefine, or even turning it off by telling Qt not to use keywords as follows:

    CONFIG += no_keywords
    

    This way, it could still be possible to use the Qt signal-slot mechanism with Q_SIGNAL, Q_SIGNALS, Q_SLOT, Q_SLOTS, Q_EMIT and all that.

    Here you can find the corresponding documentation about Q_EMIT, for instance:

    Q_EMIT

    Use this macro to replace the emit keyword for emitting signals, when you want to use Qt Signals and Slots with a 3rd party signal/slot mechanism.

    The macro is normally used when no_keywords is specified with the CONFIG variable in the .pro file, but it can be used even when no_keywords is not specified.