Scope of objects sent in connect statements



  • Hello, I am not sure about the lifespan of objects created in a function and emitted in a signal.

    int foo()
    {
        QStringList str
        //Calculations and other stuff...
       
       emit CalculationsDone(str)
       return 0;
    }
    
    void someSlot(const QStringList&)
    {
    ...
    }
    
    int bar()
    {
        connect(&objectWithFunctionFoo,SIGNAL(CalculationsDone(const QStringList&)),this,SLOT(someSlot(const QStringList&)));
    }
    

    What is the lifespan of str. Does it wait for SLOT to complete before going out of scope?


  • Qt Champions 2016

    hi
    "
    When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call. When this happens, the signals and slots mechanism is totally independent of any GUI event loop. Execution of the code following the emit statement will occur once all slots have returned. The situation is slightly different when using queued connections; in such a case, the code following the emit keyword will continue immediately, and the slots will be executed later."

    http://doc.qt.io/qt-5.5/signalsandslots.html


Log in to reply
 

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