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. Guard sentinels for slots?
Qt 6.11 is out! See what's new in the release blog

Guard sentinels for slots?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 516 Views 2 Watching
  • 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by Kent-Dorfman
    #1

    Back in the Qt4 days I occasionally ran across references that recommended a guard around slots to prohibit the slot from being recursively called. Here's a quick implementation:

    MyQObject::MySlot() {
        static bool inSlot(false);    // probably better to make this a member variable
        if (!inSlot) {
            inSlot = true;
            // *** do important slot stuff ***
            // maybe emit signals that in a round-about way 
            // cause this slot to be executed again?
            inSlot = false;
        }
    }
    

    Is this still a thing? Does Qt5 have any internal checks to make this kind of blocking redundant? Being an admittedly paranoid person, I still frequently do this to protect from signal/slot loops causing a stack overflow.

    What's the best practice and caveats these days?

    The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

    JKSHJ JonBJ 2 Replies Last reply
    0
    • Kent-DorfmanK Kent-Dorfman

      Back in the Qt4 days I occasionally ran across references that recommended a guard around slots to prohibit the slot from being recursively called. Here's a quick implementation:

      MyQObject::MySlot() {
          static bool inSlot(false);    // probably better to make this a member variable
          if (!inSlot) {
              inSlot = true;
              // *** do important slot stuff ***
              // maybe emit signals that in a round-about way 
              // cause this slot to be executed again?
              inSlot = false;
          }
      }
      

      Is this still a thing? Does Qt5 have any internal checks to make this kind of blocking redundant? Being an admittedly paranoid person, I still frequently do this to protect from signal/slot loops causing a stack overflow.

      What's the best practice and caveats these days?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Kent-Dorfman said in Guard sentinels for slots?:

      Does Qt5 have any internal checks to make this kind of blocking redundant?

      I don't think so.

      The techniques you used in Qt 4 are still valid for Qt 5.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      4
      • Kent-DorfmanK Kent-Dorfman

        Back in the Qt4 days I occasionally ran across references that recommended a guard around slots to prohibit the slot from being recursively called. Here's a quick implementation:

        MyQObject::MySlot() {
            static bool inSlot(false);    // probably better to make this a member variable
            if (!inSlot) {
                inSlot = true;
                // *** do important slot stuff ***
                // maybe emit signals that in a round-about way 
                // cause this slot to be executed again?
                inSlot = false;
            }
        }
        

        Is this still a thing? Does Qt5 have any internal checks to make this kind of blocking redundant? Being an admittedly paranoid person, I still frequently do this to protect from signal/slot loops causing a stack overflow.

        What's the best practice and caveats these days?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @Kent-Dorfman
        As @JKSH says you are still responsible for writing explicit code if you want this check. Doubtless you're well aware already, but (a) you could have this in debug/development code only if you trust to skip it in production and (b) personally in some shape or form I'd implement via, say, a function-scoped local variable whose constructor sets and destructor clears the inSlot so that you don't have to worry about putting a return in the middle of your code or an exception being raised etc.

        1 Reply Last reply
        0

        • Login

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