Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to send signal between other files in C++

    QML and Qt Quick
    3
    6
    240
    Loading More Posts
    • 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
      w.tkm last edited by 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";
      }
      
      KroMignon 1 Reply Last reply Reply Quote 0
      • KroMignon
        KroMignon @w.tkm last edited by

        @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 Reply Quote 0
        • KroMignon
          KroMignon @w.tkm last edited by

          @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 Reply Quote 1
          • W
            w.tkm @KroMignon last edited by

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

            jsulm KroMignon 2 Replies Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @w.tkm last edited by

              @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 Reply Quote 1
              • KroMignon
                KroMignon @w.tkm last edited by

                @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 Reply Quote 0
                • W
                  w.tkm @KroMignon last edited by

                  @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 Reply Quote 0
                  • First post
                    Last post