From 837524b69fdc9c963564064789b4a5e0724b4317 Mon Sep 17 00:00:00 2001 From: jupfi Date: Tue, 12 Mar 2024 11:08:23 +0100 Subject: [PATCH] Fixed bug with widget creation. --- .../base_spectrometer_view.py | 1 + src/nqrduck_spectrometer/settings.py | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/nqrduck_spectrometer/base_spectrometer_view.py b/src/nqrduck_spectrometer/base_spectrometer_view.py index 72cf6ec..b527231 100644 --- a/src/nqrduck_spectrometer/base_spectrometer_view.py +++ b/src/nqrduck_spectrometer/base_spectrometer_view.py @@ -54,6 +54,7 @@ class BaseSpectrometerView(ModuleView): setting_label.setMinimumWidth(200) edit_widget = setting.get_widget() + logger.debug("Setting widget: %s", edit_widget) # Add a icon that can be used as a tooltip if setting.description is not None: diff --git a/src/nqrduck_spectrometer/settings.py b/src/nqrduck_spectrometer/settings.py index 2ae4bb5..f36f3ad 100644 --- a/src/nqrduck_spectrometer/settings.py +++ b/src/nqrduck_spectrometer/settings.py @@ -103,7 +103,8 @@ class BooleanSetting(Setting): """ widget = QCheckBox() widget.setChecked(self.value) - widget.stateChanged.connect(lambda x=widget, s=self: s.on_value_changed(x.text())) + widget.stateChanged.connect(lambda x=widget, s=self: s.on_value_changed(bool(x))) + return widget class SelectionSetting(Setting): """ A setting that is a selection from a list of options.""" @@ -138,7 +139,7 @@ class SelectionSetting(Setting): widget = QComboBox() widget.addItems(self.options) widget.setCurrentText(self.value) - widget.currentTextChanged.connect(lambda x=widget, s=self: s.on_value_changed(x.text())) + widget.currentTextChanged.connect(lambda x=widget, s=self: s.on_value_changed(x)) return widget class IPSetting(Setting): @@ -177,4 +178,17 @@ class StringSetting(Setting): except ValueError: raise ValueError("Value must be a string") - self.settings_changed.emit() \ No newline at end of file + self.settings_changed.emit() + + def get_widget(self): + """Return a widget for the setting. + The default widget is simply a QLineEdit. + This method can be overwritten by subclasses to return a different widget. + + Returns: + QLineEdit: A QLineEdit widget that can be used to change the setting. + """ + widget = QLineEdit(str(self.value)) + widget.setMinimumWidth(100) + widget.editingFinished.connect(lambda x=widget, s=self: s.on_value_changed(x.text())) + return widget \ No newline at end of file