Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

emit or not to emit ?



  • QtDesigner is relatively nice GUI tool to implement "events" , in other terminology "callback" , in Qt terminology SIGNAL / SLOT.
    Also SIGNAL and SLOT are tied together / work together in "connect" function.

    So we basically have an "event" and Qt calls it SIGNAL .

    Now Qt "invents" yet another term " emit " which somebody "defines" this way :

    The signal is only emitted when someone calls the valueChanged() function.

    Huh?

    So what is "emit" for real ?

    1. "emit" is a macro
    2. when (emit) function is called it "emits" SIGNAL?
    3. The (emit ) function , in example valueChanged is
    where the valueChanged is the signal within the class definition **(you do not define that function because it is kinder like a virtual function**).
    

    ???

    So why are there all these convoluted "definitions" when
    emit (marco) results in function generating SIGNAL , such SIGNAL being "connected" to SLOT function to allow to process desired task ?
    ( same as "push button " clicked works ...)

    Why am I being sidetracked to "moc" which even QT IDE has hard time handling without posting obscure messages - such as " file was modified outside of.... do I want save it ?... )

    Is there a real "emit" tutorial ?
    End of rant



  • @AnneRanch
    As we said some time ago, the emit macro simply expands to nothing at all. So

    emit someSignal();
    

    expands 100% to

    someSignal();
    

    and that just executes the function. Which, because you marked it in the signals section of your class definition, does some code making it a signal.

    Using the word emit in source code is purely so it looks nice, and you have something to search for. If you never wrote emit it would make absolutely no difference to behaviour.


  • Lifetime Qt Champion

    The emit macro is just visual sugar so one can see if a signal is called or if it's a normal function call. It's not needed at all for the compiler or to make something work.


  • Lifetime Qt Champion

    Hi,

    "emit" is an empty macro that is there to make it clear in the code that a signal is going to be emitted.

    Do not mix events and signals and slots. They are two different concepts.

    Do not save moc files, they are generated from your code and should not be modified. If you have them opened in your editor then it's normal to have these warnings.

    If you have them in your source folder then it means that you are not using out of source builds or are forcing the location for the moc outputs neither of which is a good idea.

    In any case, there's a full chapter dedicated to signals and slots in Qt's documentation.


  • Moderators

    @SGaist said in emit or not to emit ?:

    Do not mix events and signals and slots. They are two different concepts.

    Agreed. You can find out more about Qt events here: https://doc.qt.io/qt-5/eventsandfilters.html

    Signals and slots are also far more flexible than callbacks.

    • Events require specialized event handlers. The handler must know exactly what kind of event to expect, so you get tight coupling.
    • For callbacks, you can usually only register 1 callback function per "trigger".

    With signals and slots however, you have loose coupling. Any C++ function can be used as a slot, if you really want. Also, multiple signals can be connected to the same slot, and multiple slots can be connected to the same signal.


Log in to reply