diff --git a/qtapputils/qthelpers.py b/qtapputils/qthelpers.py index 86c40df..2b60026 100644 --- a/qtapputils/qthelpers.py +++ b/qtapputils/qthelpers.py @@ -13,8 +13,10 @@ from typing import TYPE_CHECKING, Callable if TYPE_CHECKING: from qtpy.QtGui import QIcon + from qtapputils.widgets.waitingspinner import WaitingSpinner # ---- Standard imports +from contextlib import contextmanager import sys import platform from math import pi @@ -26,9 +28,6 @@ QWidget, QSizePolicy, QToolButton, QApplication, QStyleFactory, QAction, QToolBar) -# --- Local imports -from qtapputils.widgets import WaitingSpinner - def qbytearray_to_hexstate(qba): """Convert QByteArray object to a str hexstate.""" @@ -173,6 +172,8 @@ def create_waitspinner( spinner : WaitingSpinner The waitspinner created with the specified parameters """ + from qtapputils.widgets.waitingspinner import WaitingSpinner + dot_padding = 1 # To calculate the size of the dots, we need to solve the following @@ -254,3 +255,14 @@ def get_default_contents_margins() -> list[int, int, int, int]: style.pixelMetric(style.PM_LayoutRightMargin), style.pixelMetric(style.PM_LayoutBottomMargin), ] + + +@contextmanager +def block_signals(widget): + """Temporarily block signals for the given widget.""" + was_blocked = widget.signalsBlocked() + widget.blockSignals(True) + try: + yield + finally: + widget.blockSignals(was_blocked) diff --git a/qtapputils/widgets/range.py b/qtapputils/widgets/range.py index 03d2ab1..42b2d17 100644 --- a/qtapputils/widgets/range.py +++ b/qtapputils/widgets/range.py @@ -7,11 +7,16 @@ # Licensed under the terms of the MIT License. # ----------------------------------------------------------------------------- + # ---- Third party imports from qtpy.QtCore import Signal, QObject, QLocale, Qt from qtpy.QtGui import QValidator from qtpy.QtWidgets import QDoubleSpinBox, QWidget +# ---- Local imports +from qtapputils.qthelpers import block_signals + + LOCALE = QLocale() @@ -112,15 +117,13 @@ def value(self) -> float: def setValue(self, new_value: float): """Set the value without losing precision due to display rounding.""" old_value = self.value() - new_value = max(min(new_value, self.maximum()), self.minimum()) + new_value = float(max(min(new_value, self.maximum()), self.minimum())) - was_blocked = self.signalsBlocked() - self.blockSignals(True) - super().setValue(new_value) - self.blockSignals(was_blocked) + with block_signals(self): + super().setValue(new_value) if self._precise: - self._true_value = float(new_value) + self._true_value = new_value if old_value != self.value(): self.sig_value_changed.emit(self.value())