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

Connect multiple slots to one signal



  • Hello,
    Is it possible to connect more than on slot to a signal. For example in my program i do those two connect:

        connect(systecDev,&QCanBusDevice::framesReceived,this,&PowerSupplyControl::fillDeviceDatas);
    //////
        connect(systecDev, &QCanBusDevice::framesReceived, this, &DevicesManagement::processReceivedFrames);
    
    
    

    But only one of my slot is executed.
    Anyone can Help pls?


  • Lifetime Qt Champion

    @Babs It is of course possible to connect multiple slots to one signal - that's one of the essences of Signals&Slots.

    If you, however, react to this signal with a method like readFrames(), then the second slot will have nothing to read, as the buffer is already empty.

    So depending on your need you can either call other methods from your first slot, or emit a signal, or...

    Regards


  • Moderators

    @Babs

    So, is PowerSupplyControl a base class of DevicesManagement or vice versa ?

    do you really want to call the base class slot?



  • @aha_1980 thank you .
    Indeed i read to this signal with readFrame(). For my function processReceivedFrame() i store the received frames on a vector like this:

    void DevicesManagement::processReceivedFrames()
    {
    
        while(systecDev->framesAvailable()){
            QCanBusFrame frame=systecDev->readFrame();
            qDebug()<<frame.toString();
            QString str=frame.payload().toHex().toUpper();
            receivedMessages.push_back(str);
            emit messagesListModified();
    
        }
    }
    

    And in my second method fillDeviceDatas, i browse my vector in order to store all the parameters receive in their respective variable.

    void PowerSupplyControl::fillDeviceDatas()
    {
        for(auto &payload:receivedMessages){
            quint16 entry=(payload.left(6).right(2) + payload.left(4).right(2)).toUShort(nullptr,16);
            quint8 subEntry=static_cast<quint8>(payload.left(8).right(2).toUShort(nullptr,16));
            if(payload.startsWith("43")){
                QString message=payload.right(8);
                switch (entry) {
                case SIMMER_INDEX:
                    updateSimmerObjects(subEntry,message);
                    break;
    
                case CHARGE_INDEX:
                    updateChargeObjects(subEntry,message);
                    break;
    
                case FAN_INDEX:
                    UpdateFANObjects(subEntry,message);
                    break;
    
                case SYNC_INDEX:
                    updateSynchroDelay(message);
                    break;
    
                case DEV_PROFILE_INDEX:
                    updateDeviceProfile(message);
                    break;
                default:
                    break;
                case PDOTX1_MAP_PARAMETER:
                    updatePDOMappRData(subEntry,message,1);
                    break;
    
                case PDOTX2_MAP_PARAMETER:
                    updatePDOMappRData(subEntry,message,2);
                    break;
    
                case PDOTX3_MAP_PARAMETER:
                    updatePDOMappRData(subEntry,message,3);
                    break;
    
                case PDOTX4_MAP_PARAMETER:
                    updatePDOMappRData(subEntry,message,4);
                    break;
                }
            }
        }
    }
    

    In qml part i send read requests to my device and it response successfuly. May you have an idea of why my second slot fillDeviceDatas() is not executed.



  • @J.Hilk No there is no Inheritance relationship between those two classes.


  • Moderators

    @Babs and those 2 connects are in the same class ?



  • @J.Hilk no the first is in DeviceManagement and the second in PowerSupplyControl


  • Moderators

    @Babs and you're sure, that systecDev is a valid- in fact the same - instance in both classes ?



  • @J.Hilk systecDev is a global



  • @J.Hilk I found out what my problem was. My two slot are excuted. The problem was that it didn't pass a certain condition in the second slot. My problem is solved. Thank you for your responses.
    Best Regards,


Log in to reply