diff --git a/examples/demo_widget.py b/examples/demo_widget.py index 6ae80ee..c3213f0 100644 --- a/examples/demo_widget.py +++ b/examples/demo_widget.py @@ -29,7 +29,7 @@ QSlider::sub-page:horizontal { } QRangeSlider { - qproperty-barColor: "qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #227, stop:1 #77a)"; + qproperty-barColor: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #227, stop:1 #77a); } """ diff --git a/qtrangeslider/_qrangeslider.py b/qtrangeslider/_qrangeslider.py index efcc303..dd2dc9a 100644 --- a/qtrangeslider/_qrangeslider.py +++ b/qtrangeslider/_qrangeslider.py @@ -213,12 +213,12 @@ class QRangeSlider(QSlider): return opt def _getBarColor(self): - return self._style.brush_active or "" + return self._style.brush(self._getStyleOption()) def _setBarColor(self, color): self._style.brush_active = color - barColor = Property(str, _getBarColor, _setBarColor) + barColor = Property(QtGui.QBrush, _getBarColor, _setBarColor) def _drawBar(self, painter: QStylePainter, opt: QStyleOptionSlider): diff --git a/qtrangeslider/_style.py b/qtrangeslider/_style.py index 58d63f3..17cd957 100644 --- a/qtrangeslider/_style.py +++ b/qtrangeslider/_style.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Union from .qtcompat import PYQT_VERSION from .qtcompat.QtCore import Qt from .qtcompat.QtGui import ( + QBrush, QColor, QGradient, QLinearGradient, @@ -34,26 +35,28 @@ class RangeSliderStyle: h_offset: float = None has_stylesheet: bool = False - def brush(self, opt: QStyleOptionSlider) -> Union[QGradient, QColor]: + def brush(self, opt: QStyleOptionSlider) -> QBrush: cg = opt.palette.currentColorGroup() attr = { QPalette.Active: "brush_active", # 0 QPalette.Disabled: "brush_disabled", # 1 QPalette.Inactive: "brush_inactive", # 2 }[cg] - val = getattr(self, attr) or getattr(SYSTEM_STYLE, attr) - if isinstance(val, str): - _val = QColor(val) - if not _val.isValid(): - _val = parse_color(val) - - if not _val: + _val = getattr(self, attr) or getattr(SYSTEM_STYLE, attr) + if _val is None: return Qt.NoBrush - if opt.tickPosition != QSlider.NoTicks: - _val.setAlphaF(self.tick_bar_alpha or SYSTEM_STYLE.tick_bar_alpha) + if isinstance(_val, str): + val = QColor(_val) + if not val.isValid(): + val = parse_color(_val, default_attr=attr) + else: + val = _val - return _val + if opt.tickPosition != QSlider.NoTicks: + val.setAlphaF(self.tick_bar_alpha or SYSTEM_STYLE.tick_bar_alpha) + + return QBrush(val) def pen(self, opt: QStyleOptionSlider) -> Union[Qt.PenStyle, QColor]: cg = opt.palette.currentColorGroup() @@ -211,7 +214,7 @@ rgba_pattern = re.compile( ) -def parse_color(color: str) -> Union[QColor, QGradient]: +def parse_color(color: str, default_attr) -> Union[QColor, QGradient]: qc = QColor(color) if qc.isValid(): return qc @@ -238,7 +241,7 @@ def parse_color(color: str) -> Union[QColor, QGradient]: return grad # fallback to dark gray - return QColor("#333") + return QColor(getattr(SYSTEM_STYLE, default_attr)) def update_styles_from_stylesheet(obj: "QRangeSlider"):