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. Qt Observer-Type Pattern Using Signals and Slots
Forum Updated to NodeBB v4.3 + New Features

Qt Observer-Type Pattern Using Signals and Slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.8k Views 1 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by webzoid
    #1

    I'm interested in finding from others how you have solved a pretty simple problem.

    I have many QObjects and QWidgets. Each of these has a data buffer, varying in type depending on what information it is to hold. All data comes from a single repository but specific data gets farmed out to these QObjects and QWidgets.

    At certain points in time, I want to clear all buffers from all objects pseudo-instantaneously.

    I've thought about having a base class similar to the following pseudo-code:

    class BaseClassController {
        function registerBaseClass(BaseClass *obj) {
            connect(this, SIGNAL(clearBuffers()), obj, SLOT(clearBuffers());
        }
    
        private BaseClassController() {
    
        }
    
        static BaseClassController& instance() {
            static BaseClassController *inst = nullptr;
            if (!inst)
                inst = new BaseClassController();
            return *inst;
        }
    
        signal clearBuffers();
    }
    
    class BaseClass {
        BaseClass() {
            BaseClassController::instance().registerBaseClass(this);
        }    
    
        virtual slot clearBuffers() {
            // Clears the buffer for this instance
        }
    }
    

    Then when I want to clear ALL buffers, I just need to emit clearBuffers from the BaseClassController

    Obviously, the singleton here isn't thread-safe but would this be an obvious way to achieve what I want or is there a much better solution or a more out-the-box solution to this? Or am I barking completely up the wrong tree?

    Thanks

    jsulmJ J.HilkJ kshegunovK 3 Replies Last reply
    0
    • webzoidW webzoid

      I'm interested in finding from others how you have solved a pretty simple problem.

      I have many QObjects and QWidgets. Each of these has a data buffer, varying in type depending on what information it is to hold. All data comes from a single repository but specific data gets farmed out to these QObjects and QWidgets.

      At certain points in time, I want to clear all buffers from all objects pseudo-instantaneously.

      I've thought about having a base class similar to the following pseudo-code:

      class BaseClassController {
          function registerBaseClass(BaseClass *obj) {
              connect(this, SIGNAL(clearBuffers()), obj, SLOT(clearBuffers());
          }
      
          private BaseClassController() {
      
          }
      
          static BaseClassController& instance() {
              static BaseClassController *inst = nullptr;
              if (!inst)
                  inst = new BaseClassController();
              return *inst;
          }
      
          signal clearBuffers();
      }
      
      class BaseClass {
          BaseClass() {
              BaseClassController::instance().registerBaseClass(this);
          }    
      
          virtual slot clearBuffers() {
              // Clears the buffer for this instance
          }
      }
      

      Then when I want to clear ALL buffers, I just need to emit clearBuffers from the BaseClassController

      Obviously, the singleton here isn't thread-safe but would this be an obvious way to achieve what I want or is there a much better solution or a more out-the-box solution to this? Or am I barking completely up the wrong tree?

      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @webzoid Sounds reasonable

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • webzoidW webzoid

        I'm interested in finding from others how you have solved a pretty simple problem.

        I have many QObjects and QWidgets. Each of these has a data buffer, varying in type depending on what information it is to hold. All data comes from a single repository but specific data gets farmed out to these QObjects and QWidgets.

        At certain points in time, I want to clear all buffers from all objects pseudo-instantaneously.

        I've thought about having a base class similar to the following pseudo-code:

        class BaseClassController {
            function registerBaseClass(BaseClass *obj) {
                connect(this, SIGNAL(clearBuffers()), obj, SLOT(clearBuffers());
            }
        
            private BaseClassController() {
        
            }
        
            static BaseClassController& instance() {
                static BaseClassController *inst = nullptr;
                if (!inst)
                    inst = new BaseClassController();
                return *inst;
            }
        
            signal clearBuffers();
        }
        
        class BaseClass {
            BaseClass() {
                BaseClassController::instance().registerBaseClass(this);
            }    
        
            virtual slot clearBuffers() {
                // Clears the buffer for this instance
            }
        }
        

        Then when I want to clear ALL buffers, I just need to emit clearBuffers from the BaseClassController

        Obviously, the singleton here isn't thread-safe but would this be an obvious way to achieve what I want or is there a much better solution or a more out-the-box solution to this? Or am I barking completely up the wrong tree?

        Thanks

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @webzoid I agree with @jsulm the aproach makes sense, and its the way I'm doing it in my current project as well.

        PS: TO all those native speakers, is "That makes sense" a valid idiom? I always thought it was just a bad translation from my language, but I'm hearing it more and more in all kinds of media.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • webzoidW Offline
          webzoidW Offline
          webzoid
          wrote on last edited by
          #4

          Thanks guys. I'll use this approach.

          @J-Hilk - "that makes sense" is a perfectly valid phrase, if not an idiom. Its used a lot here in the UK.

          1 Reply Last reply
          1
          • webzoidW webzoid

            I'm interested in finding from others how you have solved a pretty simple problem.

            I have many QObjects and QWidgets. Each of these has a data buffer, varying in type depending on what information it is to hold. All data comes from a single repository but specific data gets farmed out to these QObjects and QWidgets.

            At certain points in time, I want to clear all buffers from all objects pseudo-instantaneously.

            I've thought about having a base class similar to the following pseudo-code:

            class BaseClassController {
                function registerBaseClass(BaseClass *obj) {
                    connect(this, SIGNAL(clearBuffers()), obj, SLOT(clearBuffers());
                }
            
                private BaseClassController() {
            
                }
            
                static BaseClassController& instance() {
                    static BaseClassController *inst = nullptr;
                    if (!inst)
                        inst = new BaseClassController();
                    return *inst;
                }
            
                signal clearBuffers();
            }
            
            class BaseClass {
                BaseClass() {
                    BaseClassController::instance().registerBaseClass(this);
                }    
            
                virtual slot clearBuffers() {
                    // Clears the buffer for this instance
                }
            }
            

            Then when I want to clear ALL buffers, I just need to emit clearBuffers from the BaseClassController

            Obviously, the singleton here isn't thread-safe but would this be an obvious way to achieve what I want or is there a much better solution or a more out-the-box solution to this? Or am I barking completely up the wrong tree?

            Thanks

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @webzoid said in Qt Observer-Type Pattern Using Signals and Slots:

            At certain points in time, I want to clear all buffers from all objects pseudo-instantaneously.

            You really need to have a global variable to do that? What's wrong with propagating a signal to the interested parties? Your code is going to work (provided you fix the concurrency with the singleton), but I question the wisdom of introducing an application-global state ...

            Obviously, the singleton here isn't thread-safe but would this be an obvious way to achieve what I want or is there a much better solution or a more out-the-box solution to this?

            It's also a memory leak. You could solve both by creating it on the stack, as it's just a utility object that does nothing in its constructor.

            Read and abide by the Qt Code of Conduct

            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