Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to send signal between other files in C++

How to send signal between other files in C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 652 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.
  • W Offline
    W Offline
    w.tkm
    wrote on last edited by w.tkm
    #1

    Hello.

    I want to send a signal from CppA to CppB, but I don't know how to do it.
    How can I do this?

    CppA

    void CppA::sigSend(){
        emit sig();
    }
    

    CppB

    CppB::CppB(QObject *parent): QObject(parent)
    {
        CppA* cppA = new CppA(this);
        connect(cppA, &CppA::sig, this, &CppB::slotRecv);
    }
    
    void CppB::slotRecv()
    {
        qDebug() << "OK";
    }
    
    KroMignonK 1 Reply Last reply
    0
    • W w.tkm

      @KroMignon
      I'm asking because I couldn't figure it out after reading...
      I tried changing some of the code, but it still doesn't work.

      CppB::CppB()
      {
          CppA cppA;
          connect(&cppA, &CppA::sig, this, &CppB::slotRecv);
      }
      

      Also, CppA::sigSend is executed in one second cycle using Timer in QML.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #5

      @w-tkm said in How to send signal between other files in C++:

      Also, CppA::sigSend is executed in one second cycle using Timer in QML.

      I don't think so, you have declared cppA as a local variable in CppB constructor.
      At constructor end, this variable will be destroyed, so it cannot generate any signal.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      W 1 Reply Last reply
      0
      • W w.tkm

        Hello.

        I want to send a signal from CppA to CppB, but I don't know how to do it.
        How can I do this?

        CppA

        void CppA::sigSend(){
            emit sig();
        }
        

        CppB

        CppB::CppB(QObject *parent): QObject(parent)
        {
            CppA* cppA = new CppA(this);
            connect(cppA, &CppA::sig, this, &CppB::slotRecv);
        }
        
        void CppB::slotRecv()
        {
            qDebug() << "OK";
        }
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @w-tkm said in How to send signal between other files in C++:

        I want to send a signal from CppA to CppB, but I don't know how to do it.
        How can I do this?

        It is not that complicated, but you should first take time to read at least how it works: https://doc.qt.io/qt-5/signalsandslots.html

        If you still have trouble, explain us what did not work and you will got help.
        Doing in reverse order will not help you.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        W 1 Reply Last reply
        1
        • KroMignonK KroMignon

          @w-tkm said in How to send signal between other files in C++:

          I want to send a signal from CppA to CppB, but I don't know how to do it.
          How can I do this?

          It is not that complicated, but you should first take time to read at least how it works: https://doc.qt.io/qt-5/signalsandslots.html

          If you still have trouble, explain us what did not work and you will got help.
          Doing in reverse order will not help you.

          W Offline
          W Offline
          w.tkm
          wrote on last edited by
          #3

          @KroMignon
          I'm asking because I couldn't figure it out after reading...
          I tried changing some of the code, but it still doesn't work.

          CppB::CppB()
          {
              CppA cppA;
              connect(&cppA, &CppA::sig, this, &CppB::slotRecv);
          }
          

          Also, CppA::sigSend is executed in one second cycle using Timer in QML.

          jsulmJ KroMignonK 2 Replies Last reply
          0
          • W w.tkm

            @KroMignon
            I'm asking because I couldn't figure it out after reading...
            I tried changing some of the code, but it still doesn't work.

            CppB::CppB()
            {
                CppA cppA;
                connect(&cppA, &CppA::sig, this, &CppB::slotRecv);
            }
            

            Also, CppA::sigSend is executed in one second cycle using Timer in QML.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @w-tkm said in How to send signal between other files in C++:

            CppA cppA;

            cppA is a local variable and is destroyed when CppB::CppB() finishes...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • W w.tkm

              @KroMignon
              I'm asking because I couldn't figure it out after reading...
              I tried changing some of the code, but it still doesn't work.

              CppB::CppB()
              {
                  CppA cppA;
                  connect(&cppA, &CppA::sig, this, &CppB::slotRecv);
              }
              

              Also, CppA::sigSend is executed in one second cycle using Timer in QML.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @w-tkm said in How to send signal between other files in C++:

              Also, CppA::sigSend is executed in one second cycle using Timer in QML.

              I don't think so, you have declared cppA as a local variable in CppB constructor.
              At constructor end, this variable will be destroyed, so it cannot generate any signal.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              W 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @w-tkm said in How to send signal between other files in C++:

                Also, CppA::sigSend is executed in one second cycle using Timer in QML.

                I don't think so, you have declared cppA as a local variable in CppB constructor.
                At constructor end, this variable will be destroyed, so it cannot generate any signal.

                W Offline
                W Offline
                w.tkm
                wrote on last edited by
                #6

                @KroMignon
                Thanks,I managed to make it work.
                Finally, I realized that I need to connect at the CppA where the signal is sent.

                CppA::CppA(QObject *parent): QObject(parent)
                {
                    cppB = new CppB();
                    connect( this, &CppA::sig, cppB, &CppB::slotRecv);
                }
                
                1 Reply Last reply
                0

                • Login

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