⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

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?

  • Lifetime Qt Champion

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