Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 707
otisonoza Avatar asked Apr 12 '14 21:04

otisonoza


3 Answers

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.

like image 197
lpapp Avatar answered Nov 16 '22 22:11

lpapp


There is no difference. In fact, emit is defined as an empty macro, so just

someSignal(value);

is also equivalent. The emit just makes it more obvious you're triggering a Qt signal.

like image 28
aschepler Avatar answered Nov 16 '22 23:11

aschepler


There is absolutely no difference. It's an empty preprocessor definition and thus gets removed before the compiler gets to see the code. As far as the compiler's concerned, it doesn't even exist.

It exists purely for the benefit of the programmer, to let him/her know that at that point, a signal could possibly be processed and might help in debugging of code.

like image 1
RobbieE Avatar answered Nov 16 '22 22:11

RobbieE