Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Should i avoid Signals?

Should i avoid Signals?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 186 Views
  • 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.
  • CesarC Offline
    CesarC Offline
    Cesar
    wrote on last edited by
    #1

    Does signals have a 'heavy' impact in my program performance?

    In the background how does it really works? is it something that is constantly checked?

    Should i avoid creating lambda connections whenever possible or do they have a 'minimal' impact and its ok to use it?

    For example:

    connect(ui.Button, &SpinnerButton::signalLogin, [this]()
       {    ....    });
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Does signals have a 'heavy' impact in my program performance?

      It depends what "heavy" means for you. Is it 100ms? 10? less then 1? As always - if in doubt use a profiler.

      In the background how does it really works?

      It's a bit of a lenghty topic. For details see e.g. this 3-part article: How Qt Signals and Slots Work.
      In short when slot is called on the same thread it's just a direct function call with a bit of lookup logic overhead. If slot is to be called on another thread it is put in a queue for that thread and parameters are potentially copied (depending on their type), so there's thread synchronization involved.

      is it something that is constantly checked?

      Not at all. As mentioned above on same thread it's just a function call. On different threads it's a slot queue checked in the event loop when there's anything to call.

      Should i avoid creating lambda connections whenever possible or do they have a 'minimal' impact and its ok to use it?

      "minimal" is, again, very relative. Generally it's just a function pointer stored in the connection object, so something along the lines of std::function. Yet again, if in doubt - profile.

      connect(ui.Button, &SpinnerButton::signalLogin, [this]()

      It's a good practice to use the 4 parameter version of the connect function. Especially if you're using some objects in your lambda you should tie the lifetime of that connection to the lifetime of that object, e.g.

      connect(ui.Button, &SpinnerButton::signalLogin, this, [this]()
         {    this->doStuff();   });
      

      otherwise, if this is destroyed while button still exists the connection can be triggered and that lambda will crash.

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved