'Method to stop QThread that calls OpenCV functions

I have a simple PyQT application which, on press of a "calculate" button calls a long-running function that performs some image processing using OpenCV. To allow the GUI to remain responsive, the long-running function is executed in a QThread (I implemented this by following this excellent Real Python tutorial.

Ideally, I would like to be able to cancel the calculation (e.g. by pressing a "cancel" button). Given that the function is busy executing in OpenCV, is there a "nice"/safe way to do this? Or do we have to resort to a more brutal and risky method to kill the thread?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source