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

    @jsulm ok I tried this actually

    in //MainWindow.h

    signals:
        void canDeviceChanged(int value);
    

    in //MainWindow.cpp

    connect(this, SIGNAL(canDeviceChanged(int value )),&back, SLOT(statuspican(int value)));
    
    
    if (m_canDevice)
        emit canDeviceChanged(1); 
    

    in backend.h

    public slots:
        void statuspican(int value);
    

    in backend.cpp

    void Backend::statuspican(int value)
    {
    qDebug() << "from Mainwindow :" << value;
    }
    
    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #6

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

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

    Have you read the link a give you at least 5 times?
    This can not work ==> https://doc.qt.io/qt-5/qobject.html#connect

    EDIT
    What is statuspican2?
    According to your code, the slot name is statuspican

    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

      @jsulm ok I tried this actually

      in //MainWindow.h

      signals:
          void canDeviceChanged(int value);
      

      in //MainWindow.cpp

      connect(this, SIGNAL(canDeviceChanged(int value )),&back, SLOT(statuspican(int value)));
      
      
      if (m_canDevice)
          emit canDeviceChanged(1); 
      

      in backend.h

      public slots:
          void statuspican(int value);
      

      in backend.cpp

      void Backend::statuspican(int value)
      {
      qDebug() << "from Mainwindow :" << value;
      }
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

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

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

      Remove "value". Old style connect does only contain the parameter type, not its name.
      But you should really switch to new Qt5 connect syntax - if you then do something wrong you will get a compiler error. With old style connect you only get a runtime warning if connect fails.

      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
        #8

        so logically it should works , there is no syntax error ! ?

        jsulmJ KroMignonK 2 Replies Last reply
        0
        • D dziko147

          so logically it should works , there is no syntax error ! ?

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

          @dziko147 Did you read what I and @KroMignon wrote?

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

          1 Reply Last reply
          0
          • D dziko147

            so logically it should works , there is no syntax error ! ?

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

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

            so logically it should works , there is no syntax error ! ?

            Are you jocking?

            Nothing is correct:

            • the slot name is wrong
            • the SIGNAL() syntax is wrong ==> only signal signature means: signal name and parameter types
            • the SLOT() syntax is wrong ==> only slot signature means: slot name and parameter types

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

            D 1 Reply Last reply
            3
            • KroMignonK KroMignon

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

              so logically it should works , there is no syntax error ! ?

              Are you jocking?

              Nothing is correct:

              • the slot name is wrong
              • the SIGNAL() syntax is wrong ==> only signal signature means: signal name and parameter types
              • the SLOT() syntax is wrong ==> only slot signature means: slot name and parameter types
              D Offline
              D Offline
              dziko147
              wrote on last edited by
              #11

              @KroMignon ok could you provide a connect implementation to solve this .

              i am blocked since 3 days :/

              KroMignonK 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #12

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

                ok could you provide a connect implementation to solve this .

                @jsulm already told you what's wrong and you should switch over to the new signal/slot syntax so you get proper compile time errors instead during runtime.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • D dziko147

                  @KroMignon ok could you provide a connect implementation to solve this .

                  i am blocked since 3 days :/

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

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

                  ok could you provide a connect implementation to solve this .
                  i am blocked since 3 days :/

                  Can you read?

                  @jsulm told you what to do:

                  Remove "value". Old style connect does only contain the parameter type, not its name.

                  And in Qt documentation:

                  Note that the signal and slots parameters must not contain any variable names, only the type. E.g. the following would not work and return false:

                  // WRONG
                  QObject::connect(scrollBar, SIGNAL(valueChanged(int value)),
                               label, SLOT(setNum(int value)));
                  
                  // CORRECT
                  QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
                              label,  SLOT(setNum(int)));
                  

                  I cannot believe this take 3 days to understand.

                  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 Offline
                    D Offline
                    dziko147
                    wrote on last edited by
                    #14

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

                    KroMignonK jsulmJ 2 Replies Last reply
                    0
                    • 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 Online
                        jsulmJ Online
                        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