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. problem with emit a signal!
Forum Updated to NodeBB v4.3 + New Features

problem with emit a signal!

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.9k 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.
  • S Offline
    S Offline
    Saeed_Nowroozi
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • S Saeed_Nowroozi

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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".

      1 Reply Last reply
      6
      • C Offline
        C Offline
        CroCo
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        2

        • Login

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