TSG
TSG

Reputation: 4617

Qt5 emit syntax accepts slot name. Is slot handled through event queue

I discovered quite by accident that instead of the normal style of emitting a signal previously connected to a slot, another way to trigger the slot is:

emit(slotname());

I like that I can skip the step of creating a signal and connecting, but does this REALLY cause the slot to be handled through the event queue? Or is this just calling the slot method directly?

Upvotes: 0

Views: 259

Answers (1)

fxam
fxam

Reputation: 3982

No. emit(slotname()) equals to (slotname()), which merely calls slotname() directly without queue. And it won't automagically call other slots connected to the signal.

emit is actually a macro that evaluates to nothing. It's just a syntactic sugar to show that the code is emitting a signal. Thus the following line

emit nameChanged()

is equivalent to

nameChanged()

It's nothing special, you are actually calling the nameChanged() signal method. The difference is that you don't implement the signal method yourself. You leave Qt's moc to generate the implementation. The generated implementation will call all connected slots, directly or through queue depending on how connections were made and the executing thread.

Therefore, emit(slotname()) defeats the purpose and confuses code readers.

If you are curious, emit is defined in QtCore\qobjectdefs.h:

# define emit

Upvotes: 3

Related Questions