emit one signal for two objects



  • hi,

    we are trying to emit signal that will execute slot. the signal emitted once but the slot need to be execute twice for two different objects.

    here is the code of connecting::

    for(int i=0;i< MSStorageData.size();i++)
    {
    for(int p=0;p<processingVector.size();p++)
    {
    connect(MSStorageData.at(i).get(),SIGNAL(arrivedNewMs()),
    processingVector.at(p).get(),SLOT(newMS()),Qt::DirectConnection);

        }
    

    }

    and the emit only one time needs to execute the newMS slot of the two objects in the processingVector.

    the program crashed..
    there is someone who know what can be the problem?

    tnx!



  • I don't understand what the problem is.
    You can connect as many different objects to act on the
    specific signal as you like. You just connect all the objects
    you want to receive the signal to the object.

    connect(sender, &Sender::mySignal, receiver1, &Receiver::mySlot);
    connect(sender, &Sender::mySignal, reiceiver2, &Receiver::mySlot);
    
    sender.emitMySignal();
    


  • Right, agreed - a signal can be connected to as many slots as you like. It should work. Probably your crash is due to something else.
    Maybe the problem is in your slot function - n.b. the order in which the slot functions are called in not controlled (could be in any order, and could change from one version to another).
    Or maybe you are changing your arrays, thus invalidating the pointers to your receiver objects. If you do change your arrays, I recommend calling "disconnect" first, before changing them, then calling connect again.
    Nevertheless, I generally recommend you simplify everything until you get something working (e.g. use a couple of fixed objects rather than arrays) then make it more complicated (until it breaks :-) !



  • @LeaA
    Are you programming multi thread or single thread. You are using Qt::DirectConnection parameter. If your program is multi thread , it can be reason for crach.

    If you are not programming multi thread, can you sure that every pointer is not "null pointer" in your connect function.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.