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?

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 915 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.
  • S Offline
    S Offline
    SpaceToon
    wrote on 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
    
    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • S SpaceToon

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

        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
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        2
        • S SpaceToon

          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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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
          • jsulmJ jsulm

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

            jsulmJ S 2 Replies Last reply
            0
            • S SpaceToon

              "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?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              0
              • jsulmJ jsulm

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

                jsulmJ 1 Reply Last reply
                0
                • S SpaceToon

                  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?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • jsulmJ jsulm

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

                    jsulmJ 1 Reply Last reply
                    0
                    • S SpaceToon

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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

                        "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 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

                        • Login

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