8

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

emit someSignal(value);

vs

emit(someSignal(value));

Is there any difference?

László Papp
  • 51,870
  • 39
  • 111
  • 135
otisonoza
  • 1,334
  • 2
  • 14
  • 32

3 Answers3

11

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.

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • OK, I now figured out that indeed `moc` ignores `emit` as well (although the documentation hides that fact very well). My new theory (to not lose all the faith in the QT developers) is that an earlier implementation of the signal-slot mechanism indeed *did* need this keyword, and it was retained because of backwards compatibility (and possibly they didn't document that fact in case they'll need it again for a later version). Anyway, the fact that all their documentation uses `emit` without parentheses suggests that it is a good idea (read, more future proof) to omit them in your code as well. – celtschk Apr 13 '14 at 10:51
  • @celtschk There's no need for `moc` to use the `emit` keyword, since `moc` generates the implementation of the signal method. After all, a signal is just a method that iterates the connected slots and activates (invokes) them. IIRC, there was *never* a time in released Qt versions when the `emit` keyword was used by `moc`. That keyword is there for *you* to document *your* code. It's solely for human consumption, and I, for one, am glad that it's there. I have no idea what the documentation hides, it's pretty obvious without any documentation how signals work. It's basic C++. – Kuba hasn't forgotten Monica Apr 14 '14 at 13:53
6

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
1

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.

RobbieE
  • 4,280
  • 3
  • 22
  • 36