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

Signal and Slots :: emit()



  • Hi , I have gone through the documentation, and spent some times to understand how is the magic work between signal and slots. So far, I have already understood the signal and slots on high level abstraction. However, this "emit" pseudo-keyword is really confusing for novice guy like me. Some said its a syntactic sugar, some said there is a moc -object that works with emit(). I wanted to know how,where, when , why to apply this emit() pseudo -keyword . Perhaps we can start with the example provided in the documentation.

    void Counter::setValue(int value)
    {
        if (value != m_value) {
            m_value = value;
            emit valueChanged(value);
        }
    }
    
    Counter a, b;
        QObject::connect(&a, SIGNAL(valueChanged(int)),
                         &b, SLOT(setValue(int)));
    
        a.setValue(12);     // a.value() == 12, b.value() == 12
        b.setValue(48);     // a.value() == 12, b.value() == 48
    

    From what i understand from that code. valueChanged() this function will "track" has "m_value" changed to new value yet? If yes , then "send" the signal to the setValue this SLOT to perform operation . is my interpretation correct? if yes, why still to implement "emit valueChanged(value) " in the SLOT function ? else please correct my interpretation .Thank you


  • Lifetime Qt Champion

    Hi
    the class Counter will emit valueChanged signal when setValue is called.

    So in this case, Object A will tell object B that value was changed.

    In a real program, B might be main and A some dialog,

    Signals are used to indicate to other classes that something has happen.
    Can also give the data.

    So we need valueChanged() to tell the world , not to set the value or anything else.
    Just in this case it calls setValue. IN most other case. something else would happen in slot.


  • Moderators

    Hi,

    @QT_QT_QT said:

    However, this "emit" pseudo-keyword is really confusing for novice guy like me.

    This is the perfect blog article for you: http://woboq.com/blog/how-qt-signals-slots-work.html

    Some said its a syntactic sugar, some said there is a moc -object that works with emit(). I wanted to know how,where, when , why to apply this emit() pseudo -keyword .

    Here's the trick: emit is a C++ macro. It is defined as... *drumroll*... an empty string! See https://code.woboq.org/qt5/qtbase/src/corelib/kernel/qobjectdefs.h.html#_M/emit

    The example will work exactly the same if your code looks like this:

    void Counter::setValue(int value)
    {
        if (value != m_value) {
            m_value = value;
            valueChanged(value);
        }
    }
    

    "Emitting a signal" == "calling a function". When you call a signal function, it goes through the list of connected slots, and calls the slot functions.

    Where is this signal function defined? Remember, you declared the signal as a function in the header, but you didn't implement the function body. The body is generated by moc. After you build your project, have a look in the build folder. You'll find a file like moc_counter.cpp.

    From what i understand from that code. valueChanged() this function will "track" has "m_value" changed to new value yet?

    No. Nobody is tracking m_value.

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


Log in to reply