Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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
    

  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    @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);
    }
    

  • Moderators

    @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



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


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

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

    emitSignalClassSecondInstance

    does it really emit the signal?



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


  • Lifetime Qt Champion

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



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