sending variables dynamically using signals and slots



  • In my qt c++ I have several variables that are generated dynamically(using a for loop whose upper limit taken as user input) I want to send them to another cpp file using signal and slot mechanism! How can I achieve it?



  • @Lasith
    Please, clarify what do mean by "several items dynamically". Do they have the same type?
    Something like?:

    int upperBound =10
    for (int i = 0; i != upperBound; ++i){
        //send i to another class
    }
    

  • Qt Champions 2018

    @Lasith Well, emit a signal, pass the current value to it as parameter. Connect the signal to the slot.

    // somewhere in your code 
    connect(this, SIGNAL(mySignal(int)), otherObject, SLOT(mySlot(int)));
    for (int i = 0; i < 10; ++i)
    {
        emit mySignal(i);
    }
    
    // In the other class
    void MyObject::mySlot(const int value)
    {
        // Do something
    }
    

    Did you read http://doc.qt.io/qt-5.9/signalsandslots.html ?

    Actually you should think about the need of signals and slots in this particular case - maybe it will be much easier and faster to directly call a method from the other class instead of emitting a signal? Signals/slots are useful if you want to have loose coupling, so the sender does not need to know anything about receiver.