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. Is it possible to receive a signal from a class that was instantiated twice?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to receive a signal from a class that was instantiated twice?

Scheduled Pinned Locked Moved Solved General and Desktop
signals & slotssingleton
10 Posts 4 Posters 1.0k 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.
  • S Offline
    S Offline
    SpaceToon
    wrote on 27 Mar 2020, 12:29 last edited by
    #1

    Hey guys,
    I have a question concerning Signals & Slots. Assuiumg I have two independent classes receiveSignalA Class and receiveSignalB Class , which know nothing about each other. Each of these two classes is waiting for receiving a Signal that is emitted by the third class emitSignalClass (there are two different signals).
    Because the two classes receiveSignalA Class and receiveSignalB Class knows nothing about each other, I created in each class a instance of my emitSignalClass.
    In the emitSignalClass I emit a signal that receiveSignalA Class should receive, and it does. At another point in my emitSignalClass, I emit another different signal that receiveSignalB Class should receive, but this signal is not received. Is the problem is that I created two different instances of the emitSignalClass ? If so, it would be enough if I used the singleton pattern so that only one instance is created, right?

    //emitSignalClass.cpp
    
    emitFirstSignalFunction()
    {
          emit firstSignal();
    }
    
    emitSecondSignalFunction()
    {
          emit secondSignal();
    }
    
    //receiveSignalA.cpp
    
    //my connect statement is in the constructor
    receiveSignalA()
    {
            connect(emitSignalClassFirstInstance, &emitSignalClass::emitFirstSignalFunction, this, [=](){ qDebug() << "receivedA";});
    }
    
    //the string "receivedA"  is successfully logged in my Application output console
    
    //receiveSignalB.cpp
    
    //my connect statement is in the constructor
    receiveSignalB()
    {
            connect(emitSignalClassSecondInstance, &emitSignalClass::emitSecondSignalFunction, this, [=](){ qDebug() << "receivedB";});
    }
    
    //the string "receivedB" is not successfully logged in my Application output console so that means that it does not receive the signal
    
    J J 2 Replies Last reply 27 Mar 2020, 13:26
    0
    • S SpaceToon
      27 Mar 2020, 14:08

      @jsulm
      Well, if you ask that way, the answer is probably no.
      Then is only the FIRST instance of this class, that is created, ALWAYS the sender? (in my case emitSignalClassFirstInstance)
      Is there any help somewhere in the documentation where I can read that?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 27 Mar 2020, 14:14 last edited by
      #9

      @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

      Then is only the FIRST instance of this class, that is created, ALWAYS the sender?

      The sender is the one which sends. And you write the code. So, I'm not sure what you want to read in the documentation. You should simply use one instance of the sender, not two.

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

      1 Reply Last reply
      3
      • S SpaceToon
        27 Mar 2020, 12:29

        Hey guys,
        I have a question concerning Signals & Slots. Assuiumg I have two independent classes receiveSignalA Class and receiveSignalB Class , which know nothing about each other. Each of these two classes is waiting for receiving a Signal that is emitted by the third class emitSignalClass (there are two different signals).
        Because the two classes receiveSignalA Class and receiveSignalB Class knows nothing about each other, I created in each class a instance of my emitSignalClass.
        In the emitSignalClass I emit a signal that receiveSignalA Class should receive, and it does. At another point in my emitSignalClass, I emit another different signal that receiveSignalB Class should receive, but this signal is not received. Is the problem is that I created two different instances of the emitSignalClass ? If so, it would be enough if I used the singleton pattern so that only one instance is created, right?

        //emitSignalClass.cpp
        
        emitFirstSignalFunction()
        {
              emit firstSignal();
        }
        
        emitSecondSignalFunction()
        {
              emit secondSignal();
        }
        
        //receiveSignalA.cpp
        
        //my connect statement is in the constructor
        receiveSignalA()
        {
                connect(emitSignalClassFirstInstance, &emitSignalClass::emitFirstSignalFunction, this, [=](){ qDebug() << "receivedA";});
        }
        
        //the string "receivedA"  is successfully logged in my Application output console
        
        //receiveSignalB.cpp
        
        //my connect statement is in the constructor
        receiveSignalB()
        {
                connect(emitSignalClassSecondInstance, &emitSignalClass::emitSecondSignalFunction, this, [=](){ qDebug() << "receivedB";});
        }
        
        //the string "receivedB" is not successfully logged in my Application output console so that means that it does not receive the signal
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 27 Mar 2020, 13:26 last edited by
        #2

        @SpaceToon You receive signals from class instances not from classes.

        "I created in each class a instance of my emitSignalClass." - why?
        Usually you connect signals/slots where you have instances of the classes:

        int main()
        {
            receiveSignalA a;
            receiveSignalB b;
            emitSignalClass e;
            connect(*e, &emitSignalClass::some_signal, *a, &receiveSignalA::some_slot);
            connect(*e, &emitSignalClass::some_signal, *b, &receiveSignalB::some_slot);
        }
        

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

        S 1 Reply Last reply 27 Mar 2020, 13:37
        2
        • S SpaceToon
          27 Mar 2020, 12:29

          Hey guys,
          I have a question concerning Signals & Slots. Assuiumg I have two independent classes receiveSignalA Class and receiveSignalB Class , which know nothing about each other. Each of these two classes is waiting for receiving a Signal that is emitted by the third class emitSignalClass (there are two different signals).
          Because the two classes receiveSignalA Class and receiveSignalB Class knows nothing about each other, I created in each class a instance of my emitSignalClass.
          In the emitSignalClass I emit a signal that receiveSignalA Class should receive, and it does. At another point in my emitSignalClass, I emit another different signal that receiveSignalB Class should receive, but this signal is not received. Is the problem is that I created two different instances of the emitSignalClass ? If so, it would be enough if I used the singleton pattern so that only one instance is created, right?

          //emitSignalClass.cpp
          
          emitFirstSignalFunction()
          {
                emit firstSignal();
          }
          
          emitSecondSignalFunction()
          {
                emit secondSignal();
          }
          
          //receiveSignalA.cpp
          
          //my connect statement is in the constructor
          receiveSignalA()
          {
                  connect(emitSignalClassFirstInstance, &emitSignalClass::emitFirstSignalFunction, this, [=](){ qDebug() << "receivedA";});
          }
          
          //the string "receivedA"  is successfully logged in my Application output console
          
          //receiveSignalB.cpp
          
          //my connect statement is in the constructor
          receiveSignalB()
          {
                  connect(emitSignalClassSecondInstance, &emitSignalClass::emitSecondSignalFunction, this, [=](){ qDebug() << "receivedB";});
          }
          
          //the string "receivedB" is not successfully logged in my Application output console so that means that it does not receive the signal
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 27 Mar 2020, 13:35 last edited by
          #3

          @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

          If so, it would be enough if I used the singleton pattern so that only one instance is created, right?

          IMHO a Singleton should be a last resort not the first idea.

          But it would probably solve your issue, yes


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • J jsulm
            27 Mar 2020, 13:26

            @SpaceToon You receive signals from class instances not from classes.

            "I created in each class a instance of my emitSignalClass." - why?
            Usually you connect signals/slots where you have instances of the classes:

            int main()
            {
                receiveSignalA a;
                receiveSignalB b;
                emitSignalClass e;
                connect(*e, &emitSignalClass::some_signal, *a, &receiveSignalA::some_slot);
                connect(*e, &emitSignalClass::some_signal, *b, &receiveSignalB::some_slot);
            }
            
            S Offline
            S Offline
            SpaceToon
            wrote on 27 Mar 2020, 13:37 last edited by
            #4

            "I created in each class a instance of my emitSignalClass." - why?
            Usually you connect signals/slots where you have instances of the classes:

            Hey @jsulm tahnk you for your reply.
            In fact, my application is a little bit more complicated than described. My application consists of a Gui, which is seperated in two different views. So receiveSignalA is in fact my mainwindowA.cpp which belong to mainwindowA.ui and receiveSignalB is my mainwindowB.cpp which belong to mainwindowB.ui. These two classes only controls the associated GUIs.

            My logic is included in my emitSignalClass. Therefore I dont want my MainWindowClasses know something about eachother. they only should interact with my logic class.

            To switch between the two Uis I have used a stackedWidget.

            //in mainwindowA.cpp constructor:
               ui->stackedWidget->insertWidget(1, &mainwindowB);
            
            //other functions and so on
            
            //when I click the button to switch to the view
                ui->stackedWidget->setCurrentIndex(1);
            

            So in my first View I take data from my emitSignalClass via the first signal. Then I switch via a button to the next view and there I take data from emitSignalClass via the second signal. So my main looks different then yours:

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                MainWindowA m;
                m.show();
                return a.exec();
            }
            

            But regardless of how this is implemented: Should I still be able to receive the signals if I have multiple instances of emitSignalClass, or can there be only one instance of it?

            J S 2 Replies Last reply 27 Mar 2020, 13:43
            0
            • S SpaceToon
              27 Mar 2020, 13:37

              "I created in each class a instance of my emitSignalClass." - why?
              Usually you connect signals/slots where you have instances of the classes:

              Hey @jsulm tahnk you for your reply.
              In fact, my application is a little bit more complicated than described. My application consists of a Gui, which is seperated in two different views. So receiveSignalA is in fact my mainwindowA.cpp which belong to mainwindowA.ui and receiveSignalB is my mainwindowB.cpp which belong to mainwindowB.ui. These two classes only controls the associated GUIs.

              My logic is included in my emitSignalClass. Therefore I dont want my MainWindowClasses know something about eachother. they only should interact with my logic class.

              To switch between the two Uis I have used a stackedWidget.

              //in mainwindowA.cpp constructor:
                 ui->stackedWidget->insertWidget(1, &mainwindowB);
              
              //other functions and so on
              
              //when I click the button to switch to the view
                  ui->stackedWidget->setCurrentIndex(1);
              

              So in my first View I take data from my emitSignalClass via the first signal. Then I switch via a button to the next view and there I take data from emitSignalClass via the second signal. So my main looks different then yours:

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  MainWindowA m;
                  m.show();
                  return a.exec();
              }
              

              But regardless of how this is implemented: Should I still be able to receive the signals if I have multiple instances of emitSignalClass, or can there be only one instance of it?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 27 Mar 2020, 13:43 last edited by
              #5

              @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

              Should I still be able to receive the signals if I have multiple instances of emitSignalClass

              You will receive signals from instances you're connected to

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

              S 1 Reply Last reply 27 Mar 2020, 13:56
              0
              • J jsulm
                27 Mar 2020, 13:43

                @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

                Should I still be able to receive the signals if I have multiple instances of emitSignalClass

                You will receive signals from instances you're connected to

                S Offline
                S Offline
                SpaceToon
                wrote on 27 Mar 2020, 13:56 last edited by
                #6

                You will receive signals from instances you're connected to

                But then why my Signal is not received here ?

                //receiveSignalB.cpp
                
                //my connect statement is in the constructor
                receiveSignalB()
                {
                        connect(emitSignalClassSecondInstance, &emitSignalClass::emitSecondSignalFunction, this, [=](){ qDebug() << "receivedB";});
                }
                

                IMHO a Singleton should be a last resort not the first idea.
                But it would probably solve your issue, yes

                @J-Hilk thank you.
                What could be an alternative?

                J 1 Reply Last reply 27 Mar 2020, 13:59
                0
                • S SpaceToon
                  27 Mar 2020, 13:56

                  You will receive signals from instances you're connected to

                  But then why my Signal is not received here ?

                  //receiveSignalB.cpp
                  
                  //my connect statement is in the constructor
                  receiveSignalB()
                  {
                          connect(emitSignalClassSecondInstance, &emitSignalClass::emitSecondSignalFunction, this, [=](){ qDebug() << "receivedB";});
                  }
                  

                  IMHO a Singleton should be a last resort not the first idea.
                  But it would probably solve your issue, yes

                  @J-Hilk thank you.
                  What could be an alternative?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 27 Mar 2020, 13:59 last edited by
                  #7

                  @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

                  emitSignalClassSecondInstance

                  does it really emit the signal?

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

                  S 1 Reply Last reply 27 Mar 2020, 14:08
                  0
                  • J jsulm
                    27 Mar 2020, 13:59

                    @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

                    emitSignalClassSecondInstance

                    does it really emit the signal?

                    S Offline
                    S Offline
                    SpaceToon
                    wrote on 27 Mar 2020, 14:08 last edited by SpaceToon
                    #8

                    @jsulm
                    Well, if you ask that way, the answer is probably no.
                    Then is only the FIRST instance of this class, that is created, ALWAYS the sender? (in my case emitSignalClassFirstInstance)
                    Is there any help somewhere in the documentation where I can read that?

                    J 1 Reply Last reply 27 Mar 2020, 14:14
                    0
                    • S SpaceToon
                      27 Mar 2020, 14:08

                      @jsulm
                      Well, if you ask that way, the answer is probably no.
                      Then is only the FIRST instance of this class, that is created, ALWAYS the sender? (in my case emitSignalClassFirstInstance)
                      Is there any help somewhere in the documentation where I can read that?

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 27 Mar 2020, 14:14 last edited by
                      #9

                      @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

                      Then is only the FIRST instance of this class, that is created, ALWAYS the sender?

                      The sender is the one which sends. And you write the code. So, I'm not sure what you want to read in the documentation. You should simply use one instance of the sender, not two.

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

                      1 Reply Last reply
                      3
                      • S SpaceToon
                        27 Mar 2020, 13:37

                        "I created in each class a instance of my emitSignalClass." - why?
                        Usually you connect signals/slots where you have instances of the classes:

                        Hey @jsulm tahnk you for your reply.
                        In fact, my application is a little bit more complicated than described. My application consists of a Gui, which is seperated in two different views. So receiveSignalA is in fact my mainwindowA.cpp which belong to mainwindowA.ui and receiveSignalB is my mainwindowB.cpp which belong to mainwindowB.ui. These two classes only controls the associated GUIs.

                        My logic is included in my emitSignalClass. Therefore I dont want my MainWindowClasses know something about eachother. they only should interact with my logic class.

                        To switch between the two Uis I have used a stackedWidget.

                        //in mainwindowA.cpp constructor:
                           ui->stackedWidget->insertWidget(1, &mainwindowB);
                        
                        //other functions and so on
                        
                        //when I click the button to switch to the view
                            ui->stackedWidget->setCurrentIndex(1);
                        

                        So in my first View I take data from my emitSignalClass via the first signal. Then I switch via a button to the next view and there I take data from emitSignalClass via the second signal. So my main looks different then yours:

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            MainWindowA m;
                            m.show();
                            return a.exec();
                        }
                        

                        But regardless of how this is implemented: Should I still be able to receive the signals if I have multiple instances of emitSignalClass, or can there be only one instance of it?

                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on 30 Mar 2020, 07:12 last edited by
                        #10

                        @SpaceToon said in Is it possible to receive a signal from a class that was instantiated twice?:

                        int main(int argc, char *argv[])
                        {
                        QApplication a(argc, argv);
                        MainWindowA m;
                        m.show();
                        return a.exec();
                        }

                        Where is your MainWindowB instance create? Does MainWindowA do this somewhere?

                        In general, I would expect something along these lines in main:

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            emitSignalClass emitter;
                            MainWindowA mA(&emitter);
                            mA.show();
                            MainWindowB mB(&emitter);
                            mB.hide();
                            return a.exec();
                        }
                        

                        Create an instance of your signal-emitting class first and hand it to the constructurs. Store a pointer to the same emitter object as member variable in both MainWindowA and MainWindowB.

                        1 Reply Last reply
                        0

                        10/10

                        30 Mar 2020, 07:12

                        • Login

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