[Interest] Design question: providing loop status

Roland Hughes roland at logikalsolutions.com
Fri Apr 22 13:03:26 CEST 2022

On 4/22/22 05:00, Sean Murphy wrote:
> I've got an existing class that will be used in both Qt and non-Qt/non-UI applications. One of that functions within that class parses large text files with what is essentially a while(!file.eof()) loop. I'd like to add functionality to the class that would provide some sort of parsing status back to anyone using the class - for example "currently on loop X of N". Essentially I'd like to add the equivalent of a Qt signal.
The problem with using Qt for any length of time is that you forget how 
to do things in the real world.

Long before C++ was even a gleam in the eye of commercial compiler developers, we did this with C via function pointers.

Take a look at NanoGUI, in particular here:
line 211 where they set_callback()

After that go look for the Button class source and the set_callback() method. You need a different signature, something along the lines of

void (*call_back)( const char *msg, long percentCompleteScaled, long recordsRead, long recordsFound);

while (whatever)
    do some reading

    if (beenAWhile())
      if (call_back != nullPtr)
         call_back( "Still chewing", (recordCount/recordsRead)*100, recordsRead, recordsFound);

You will need to look up the exact syntax.

If you are designing cross architecture classes and libraries, you have to stop thinking about Qt. It will mess up your design decisions every time. I just finished a project using NanoGUI. Getting out of the Signals & Slots mindset was a tough and bitter cookie to chew. The reality is, with today's language standard you pretty much don't need them unless you need the ability to queue the execution to a different thread.

I'm not a fan of lambdas because you can return to an object that has been deleted. Having said that, if you are properly controlling the lifespan of your object they can be safe.

Oh, just to save you looking it up, here's how they defined set_callback

     /// Set the push callback (for any type of button).
     void set_callback(const std::function<void()> &callback) { m_callback = callback; }

Roland Hughes, President
Logikal Solutions


More information about the Interest mailing list