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/slot between two different classes
Forum Updated to NodeBB v4.3 + New Features

signal/slot between two different classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 5 Posters 3.3k Views 1 Watching
  • 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.
  • D dziko147

    @KroMignon even i remove the "value" it doesn't works .
    I didn't understand why .
    No error but the application spits .

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

    @dziko147 said in signal/slot between two different classes:

    No error but the application spits .

    Why not using the debugger to find out why your application crash?

    And in you connect:

    connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
    

    What is back?
    Where is it defined?

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

    1 Reply Last reply
    1
    • D dziko147

      @KroMignon even i remove the "value" it doesn't works .
      I didn't understand why .
      No error but the application spits .

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #16

      @dziko147 Next thing to check: what does connect() call return?

      qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
      

      Next: did you make sure that the signal is actually emitted?

      if (m_canDevice) {
          qDebug() << "Emit signal";
          emit canDeviceChanged(1);
      }
      

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

      D 1 Reply Last reply
      0
      • jsulmJ jsulm

        @dziko147 Next thing to check: what does connect() call return?

        qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
        

        Next: did you make sure that the signal is actually emitted?

        if (m_canDevice) {
            qDebug() << "Emit signal";
            emit canDeviceChanged(1);
        }
        
        D Offline
        D Offline
        dziko147
        wrote on last edited by
        #17

        @jsulm

        qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
        

        return true .

        And the signal is emmited

        SGaistS 1 Reply Last reply
        0
        • D dziko147

          @jsulm

          qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
          

          return true .

          And the signal is emmited

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #18

          Hi,

          @dziko147 said in signal/slot between two different classes:

          @jsulm

          qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
          

          return true .

          And the signal is emmited

          Where is back defined ?
          Any chances that this is a function local variable that is destroyed at the end of it ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            @dziko147 said in signal/slot between two different classes:

            @jsulm

            qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
            

            return true .

            And the signal is emmited

            Where is back defined ?
            Any chances that this is a function local variable that is destroyed at the end of it ?

            D Offline
            D Offline
            dziko147
            wrote on last edited by
            #19

            @SGaist

            Backend is my second class . the class which i want to get data in .
            So back is an instance of Backend .

            So i defined like this :

            Backend back; 
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #20

              The question is: where is it defined ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dziko147
                wrote on last edited by
                #21

                @SGaist

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    m_ui(new Ui::MainWindow),
                {
                    m_ui->setupUi(this);
                    
                    Backend back ;
                connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
                
                KroMignonK 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #22

                  That's what I suspected: back is destroyed at the end of the constructor.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • D dziko147

                    @SGaist

                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        m_ui(new Ui::MainWindow),
                    {
                        m_ui->setupUi(this);
                        
                        Backend back ;
                    connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
                    
                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by KroMignon
                    #23

                    @dziko147 said in signal/slot between two different classes:

                    {

                    m_ui->setupUi(this);
                    Backend back ;
                    connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
                    

                    I know that you are a C++/Qt beginner, but as many times written: please take time to understand C++ basics.

                    C++ variable scope and life-cycle is one of them:

                    • https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
                    • https://www.geeksforgeeks.org/scope-of-variables-in-c/
                    • there are many more available on internet

                    If you don't learn, how do you expect programming?

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

                    1 Reply Last reply
                    3
                    • D Offline
                      D Offline
                      dziko147
                      wrote on last edited by
                      #24

                      @KroMignon Hello
                      to create a global variable . Can i create Backend back; in MainWindow.h .

                      KroMignonK 1 Reply Last reply
                      0
                      • D dziko147

                        @KroMignon Hello
                        to create a global variable . Can i create Backend back; in MainWindow.h .

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

                        @dziko147 said in signal/slot between two different classes:

                        to create a global variable . Can i create Backend back; in MainWindow.h .

                        I don't want to be pedantic, but as you seems to be very unexperimented I will try to be as clear as possible.

                        There is no need of a global variable at all.

                        For me, there are 2 clean way to do it:

                        1. create a variable in main() and do all connection there.
                        2. add a class attribute from type Backend into MainWindow and eventually give access to it through getter.

                        My preferred way is the second.
                        But this is up to 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)

                        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