PyQT, how to use QThread to stop application from proceeding?
-
I have an application that just basically runs through a set of functions. Within those functions there is a popup window that allows the user to cancel out. I want this to stop everything that is happening. I was trying to use QThreading to do this. I start the thread with a button click using
self.thread = QThread() self.ui.pb_start.clicked.connect(lambda: self.thread.started.connect(self.start_button_click()))
The function start_button_click is just a function that makes other function calls. Something along these lines
def start_button_click(self): self.get_input_vals_from_gui() self.write_to_log_box("Starting") self.get_ranges_for_inputs() self.get_values_from_gui() self.power_meas() # <-- popup window is offered within here self.download_to_ref_file()
If the user hits 'stop' on the popup window. A function gets called that just has
def stop_program(self): self.thread.terminate() # self.thread.stop() <- also have tried this...to no avail
This function gets called, but does not stop the thread.
-
@freemanl144
You cannot do anything to or with the UI in anything other than the main thread. As a newcomer to Qt you can be sure you will want to avoid threads at all costs. -
@JonB Okay so then how should I go about stopping my processes? If the user hits the 'quit' I want to be able to stop everything that is running but leave the application up. I would have assumed that by having the actual functions running in a separate thread that would be accomplished.
-
@freemanl144
If you really have long running, non-UI processing to do go ahead and use threads and don't get it wrong. Doesn't alter the fact that threads cannot address the UI. Use signals from your threads to inform the UI thread of something for it to act on. Use a signal from your main thread to secondary threads if you want to tell them to exit. If the UI needs to allow the user to interrupt, put up a dialog and send a signal to the sub-threads (preferably) or terminate them (not so preferably).