diff --git a/src/nqrduck_spectrometer/controller.py b/src/nqrduck_spectrometer/controller.py index 031913c..41226b6 100644 --- a/src/nqrduck_spectrometer/controller.py +++ b/src/nqrduck_spectrometer/controller.py @@ -4,6 +4,7 @@ Careful - this is not the base class for the spectrometer submodules, but the co """ import logging +from PyQt6.QtCore import QObject, pyqtSignal, QThread from nqrduck.module.module_controller import ModuleController from nqrduck.core.main_controller import MainController from nqrduck_spectrometer.base_spectrometer import BaseSpectrometer @@ -17,6 +18,8 @@ class SpectrometerController(ModuleController): def __init__(self, module): """This method initializes the controller.""" super().__init__(module) + self.measurement_thread = None + self.measurement_worker = None def _load_spectrometer_modules(self) -> None: """This method loads the spectrometer (sub-)modules and adds them to the spectrometer model.""" @@ -54,7 +57,7 @@ class SpectrometerController(ModuleController): """ # This signal starts a measurement if key == "start_measurement": - self.on_measurement_start() + self.start_measurement_in_thread() # This signal sets the frequency elif key == "set_frequency": self.module.model.active_spectrometer.controller.set_frequency(value) @@ -85,3 +88,29 @@ class SpectrometerController(ModuleController): self.module.model.active_spectrometer, ) self.module.model.active_spectrometer.controller.start_measurement() + + def start_measurement_in_thread(self): + """This method starts the measurement in a separate QThread.""" + self.measurement_thread = QThread() + self.measurement_worker = MeasurementWorker(self) + self.measurement_worker.moveToThread(self.measurement_thread) + self.measurement_thread.started.connect(self.measurement_worker.run) + self.measurement_worker.finished.connect(self.measurement_thread.quit) + self.measurement_worker.finished.connect(self.measurement_worker.deleteLater) + self.measurement_thread.finished.connect(self.measurement_thread.deleteLater) + self.measurement_thread.start() + +class MeasurementWorker(QObject): + """Worker class to run the measurement in a separate thread.""" + finished = pyqtSignal() + + def __init__(self, controller): + """Initialize the worker.""" + super().__init__() + self.controller = controller + + def run(self): + """Run the measurement.""" + self.controller.on_measurement_start() + self.finished.emit() +