Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Scope of objects sent in connect statements

    General and Desktop
    scope connect signal&slot
    2
    2
    1046
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      RDiGuida last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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

        1 Reply Last reply Reply Quote 0
        • First post
          Last post