Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal and Slots :: emit()

Signal and Slots :: emit()

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 22.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QT_QT_QT
    wrote on 4 May 2016, 07:09 last edited by
    #1

    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

    J 1 Reply Last reply 4 May 2016, 10:11
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 4 May 2016, 07:19 last edited by
      #2

      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.

      1 Reply Last reply
      2
      • Q QT_QT_QT
        4 May 2016, 07:09

        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

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 4 May 2016, 10:11 last edited by JKSH 5 May 2016, 00:41
        #3

        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.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4

        1/3

        4 May 2016, 07:09

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved