When using QtConcurrent::run to run code in the background from a UI the
QFuture::then(QObject *context, Function &&function) can be used to update the UI because you can pass the window object as context to make function execute on the GUI thread. This is very convenient however there is one problem. When the window is closed and it is deallocated before the background task completes the program will crash when the task completes because the context passed to then is no longer valid.
Is there an easy way to prevent this problem? I know a QFutureWatcher can be used but that object would need to be added to the window class and one would be needed for every asynchronous task the window can execute which is cumbersome.