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

problem with emit a signal!



  • Hi All,
    I understanding signal and slot. Read about emit and it behavior. I do not understand, what is happen, when emit a signal. I see the codes just define a signal and emit this signal and use this signal in a function, slot. what read I about signal is that a signal connect to slot but emit is not like that.



  • @saeed_nowroozi

    1. Use connect() to connect signal to slot. This sets up a function (the slot) ready to be called when the signal gets emitted, that's all at this stage.

    2. At a future point some code will go emit theSignal(parameters) when something "happens". That might be in your code explicitly, or in-built Qt code (e.g. when a button gets pressed). That emits/raises the signal.

    3. Qt internals (in its main event loop) look around for any slots previously connected to that signal. Each of those signals in turn is called (with any parameters, if appropriate to the signal) to execute the slot code. When the slot code ends and returns, control is returned into the Qt event loop to see if there is another slot to fire for the signal. When all slots done, signal processing is finished. Qt event loop returns to waiting for next thing to "happen".



  • The idea is simple. Imagine you would like to know if something happens and based on it, you need to take an action. This scenario occurs in Qt via signal-slot approach. Knowing if something happens is usually carried out with emitting a signal. Taking an action is usually done via slot.

    Now imagine you have Student Class and you need to know if the name of the student has been changed. The following simple minimal working example illustrate this scenario:

    student.h

    #ifndef STUDENT_H
    #define STUDENT_H
    #include <QString>
    #include <QObject>
    
    class Student : public QObject
    {
        Q_OBJECT  // <- very important for signals and slots macros
    public:
        Student();
        Student(const QString &name, QObject *parent=nullptr);
        QString get_name() const { return m_name; }
        void setName(const QString &name);
    
    signals:
       void nameChanged( const QString &name );
    
    private slots:
       void nameChangedAction(const QString &name);
    
    private:
        QString m_name;
    };
    
    #endif // STUDENT_H
    

    student.cpp

    #include "student.h"
    #include <QDebug>
    
    Student::Student()
    {
    
    }
    
    Student::Student(const QString &name, QObject *parent) : QObject(parent), m_name(name)
    {
        QObject::connect(this, SIGNAL(nameChanged(const QString&)), this, SLOT(nameChangedAction(const QString&)) );
    }
    
    void Student::setName( const QString &name )
    {
        m_name = name;
        emit nameChanged( name );
    }
    
     void Student::nameChangedAction(const QString &name)
     {
         qDebug() << "Student's name has been changed to " << name;
     }
    

    main.cpp

    #include <QCoreApplication>
    #include "student.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Student student("Smith");
        student.setName("Bill");
    
        return a.exec();
    }
    

    Remember: you don't create a body function for a signal. You just declare it. You will get an error if you do so. Play with this example to understand the signal-slot approach.


Log in to reply