title: stopping a Qthread with a QTimer
body: <p>I am starting a <code>Qthread</code> in my GUI to perform an optimization function. I want to include a stopping function that can interrupt the <code>Qthread</code>, and end the optimization function immediately.</p>
  <p>I read that using <code>Qthread.terminate()</code> is not recommended; Using a stopping flag is not possible because the nature of the function is not a loop.</p>
  <p>I thought about using a <code>QTimer</code> in the <code>QThread</code> (a watchdog timer) that periodically checks a stopping flag, and if it is triggered, just end the optimization function, but I can not really imagine how such an idea can be written.</p>
  <p>any Ideas?</p>
  <pre><code>class Worker(QObject):
      finished = pyqtSignal()
      def run(self):
          # implement a QTimer here that will somehow interrupt the minimize function
          # minimize is an arbitrary function that takes too long to run, and uses child processes to do so
          result = minimize(something)
  
  FROM_Optimization_Process,_ = loadUiType(os.path.join(os.path.dirname(__file__),&quot;ui_files/Optimization_process_window.ui&quot;))
  
  class Optimization_process_window(QDialog, FROM_Optimization_Process):
      def __init__(self, parent=None):
          # first UI
          super(Optimization_process_window, self).__init__(parent)
          self.setupUi(self)
  
      def start_solving_thread(self):
          self.thread = QThread()
          self.thread.daemon = True
          self.worker = Worker()
          self.worker.moveToThread(self.thread)
          self.thread.started.connect(self.worker.run)
          self.worker.finished.connect(self.thread.quit)
          self.worker.finished.connect(self.worker.deleteLater)
          self.thread.finished.connect(self.thread.deleteLater)
          self.thread.start()
  
      def stop_solving(self):
          # implement an interrupt function here
          self.thread.quit()
          self.thread.wait()
  
  </code></pre>