pyqt: How to quit a thread properly
<p>I wrote an pyqt gui and used threading to run code which needs a long time to be executed, but I want to have the choice to stop the execution safely. I dont want to use the get_thread.terminate() method. I want to stop the code by a special function (maybe <strong>del</strong>()). My problem is that, I wrote the code in a own class and just want to abort the class without changing a lot of syntax.</p>

<p>Edit: It was mentioned that one has to pass a flag to the class, which has to be checked constantly. How do I send this flag to the class? Because the flag has to change the value, when one presses the stop button.</p>

<p>Edit 2: My solution so far is, to declerate a global variable with the name running_global. I changed self.get_thread.terminate() to running_global = False and I check constantly in my long_running_prog if the variable has been set False. I think this solution is ugly, so I would be pretty happy if someone has a better idea.</p>

<p><strong>This is my code for the dialog where I start the thread:</strong></p>

<pre><code>class SomeDialog(QtGui.QDialog,
     userinterface_status.Ui_status_window):
     finished = QtCore.pyqtSignal(bool)

    def __init__(self):
        """
        :param raster: Coordinates which are going to be scanned.
        """
        super(self.__class__, self).__init__()  # old version, used in python 2.
        self.setupUi(self)  # It sets up layout and widgets that are defined
        self.get_thread = SomeThread()

        # Conencting the buttons
        self.start_button.clicked.connect(self.start)
        self.stop_button.clicked.connect(self.stop)
        self.close_button.clicked.connect(self.return_main)

        # Connecting other signals
        self.connect(self.get_thread, QtCore.SIGNAL("stop()"), self.stop)
        self.connect(self.get_thread, QtCore.SIGNAL("update_status_bar()"), self.update_status_bar)

    def return_main(self):
        """
        Function is excecuted, when close button is clicked.
        """
        print("return main")
        self.get_thread.terminate()
        self.close()

    def start(self):
        """
        Starts the thread, which means that the run method of the thread is started.
        """
        self.start_button.setEnabled(False)
        self.get_thread.start()

    def stop(self):
        print("Stop programm.")
        self.start_button.setEnabled(True)
        self.get_thread.quit()

    def end(self):
        QtGui.QMessageBox.information(self, "Done!", "Programm finished")



    def closeEvent(self, event):
        """
        This method is called, when the window is closed and will send a signal to the main window to activaete the
        window again.
        :param event:
        """
        self.finished.emit(True)
        # close window
        event.accept()
</code></pre>

<p><strong>In the following class is the code for the thread:</strong></p>

<pre><code>class SomeThread(QtCore.QThread):
    finished = QtCore.pyqtSignal(bool)

    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        print("del")
        self.wait()

    def run(self):
        self.prog = long_running_prog(self.emit)  # Sending from the prog signals
        self.prog.run()
        self.prog.closeSystem()  # Leaving the programm in a safe way.
</code></pre>

<p>So if one presses the stop button, the programm should instantly shut down in a save way. Is there a way to abort the class in a save way? For example can I pass a variable to the long_running_prog class which turns True, when one presses the stop button? If somethin like this is possible, could one tell me how?</p>

<p>Thanks for your help in advance</p>

<p>I hope you understand my problem.
Greetings
Hizzy</p>

<p>This is impossible to do unless <code>prog.run(self)</code> would periodically inspect a value of a flag to break out of its loop. Once you implement it, <code>__del__(self)</code> on the thread should set the flag and only then <code>wait</code>.</p>

python qt python-3.x pyqt pyqt4