def test_change_floatslider_range(gslider: QRangeSlider, qtbot): with qtbot.waitSignals([gslider.rangeChanged, gslider.valueChanged]): gslider.setMinimum(30) assert gslider.value()[0] == 30 == gslider.minimum() assert gslider.maximum() == 99 with qtbot.waitSignal(gslider.rangeChanged): gslider.setMaximum(70) assert gslider.value()[0] == 30 == gslider.minimum() assert gslider.value()[1] == 70 == gslider.maximum() with qtbot.waitSignals([gslider.rangeChanged, gslider.valueChanged]): gslider.setRange(40, 60) assert gslider.value()[0] == 40 == gslider.minimum() assert gslider.maximum() == 60 with qtbot.waitSignal(gslider.valueChanged): gslider.setValue([40, 50]) assert gslider.value()[0] == 40 == gslider.minimum() assert gslider.value()[1] == 50 with qtbot.waitSignals([gslider.rangeChanged, gslider.valueChanged]): gslider.setMaximum(45) assert gslider.value()[0] == 40 == gslider.minimum() assert gslider.value()[1] == 45 == gslider.maximum()
def test_slider_extremes(gslider: QRangeSlider, mag, qtbot): _mag = 10**mag with qtbot.waitSignal(gslider.rangeChanged): gslider.setRange(-_mag, _mag) for i in _linspace(-_mag, _mag, 10): gslider.setValue((i, _mag)) assert math.isclose(gslider.value()[0], i, rel_tol=1e-8) gslider.initStyleOption(QStyleOptionSlider())
def test_range(qtbot, orientation): rs = QRangeSlider(getattr(Qt, orientation)) qtbot.addWidget(rs) rs.setValue([10, 20]) assert rs.value() == (10, 20) rs.setRange(15, 20) assert rs.value() == (15, 20) assert rs.minimum() == 15 assert rs.maximum() == 20
def test_float_values(gslider: QRangeSlider, qtbot): with qtbot.waitSignal(gslider.rangeChanged): gslider.setRange(0.1, 0.9) assert gslider.minimum() == 0.1 assert gslider.maximum() == 0.9 with qtbot.waitSignal(gslider.valueChanged): gslider.setValue([0.4, 0.6]) assert gslider.value() == (0.4, 0.6) with qtbot.waitSignal(gslider.valueChanged): gslider.setValue([0, 1.9]) assert gslider.value()[0] == 0.1 == gslider.minimum() assert gslider.value()[1] == 0.9 == gslider.maximum()
def test_drag_handles(qtbot): rs = QRangeSlider(Qt.Horizontal) qtbot.addWidget(rs) rs.setRange(0, 99) rs.setValue((20, 80)) rs.setMouseTracking(True) rs.show() # press the left handle pos = rs._handleRect(0).center() with qtbot.waitSignal(rs.sliderPressed): qtbot.mousePress(rs, Qt.LeftButton, pos=pos) assert rs._pressedControl == SC_HANDLE assert rs._pressedIndex == 0 # drag the left handle with qtbot.waitSignals([rs.sliderMoved] * 13): # couple less signals for _ in range(15): pos.setX(pos.x() + 2) qtbot.mouseMove(rs, pos) with qtbot.waitSignal(rs.sliderReleased): qtbot.mouseRelease(rs, Qt.LeftButton) # check the values assert rs.value()[0] > 30 assert rs._pressedControl == SC_NONE # press the right handle pos = rs._handleRect(1).center() with qtbot.waitSignal(rs.sliderPressed): qtbot.mousePress(rs, Qt.LeftButton, pos=pos) assert rs._pressedControl == SC_HANDLE assert rs._pressedIndex == 1 # drag the right handle with qtbot.waitSignals([rs.sliderMoved] * 13): # couple less signals for _ in range(15): pos.setX(pos.x() - 2) qtbot.mouseMove(rs, pos) with qtbot.waitSignal(rs.sliderReleased): qtbot.mouseRelease(rs, Qt.LeftButton) # check the values assert rs.value()[1] < 70 assert rs._pressedControl == SC_NONE
def test_drag_handles_beyond_edge(qtbot): rs = QRangeSlider(Qt.Horizontal) qtbot.addWidget(rs) rs.setRange(0, 99) rs.setValue((20, 80)) rs.setMouseTracking(True) rs.show() # press the right handle pos = rs._handleRect(1).center() with qtbot.waitSignal(rs.sliderPressed): qtbot.mousePress(rs, Qt.LeftButton, pos=pos) assert rs._pressedControl == SC_HANDLE assert rs._pressedIndex == 1 # drag the handle off the right edge and make sure the value gets to the max for _ in range(7): pos.setX(pos.x() + 10) qtbot.mouseMove(rs, pos) with qtbot.waitSignal(rs.sliderReleased): qtbot.mouseRelease(rs, Qt.LeftButton) assert rs.value()[1] == 99
from qtrangeslider import QRangeSlider from qtrangeslider.qtcompat.QtCore import Qt from qtrangeslider.qtcompat.QtWidgets import QApplication app = QApplication([]) slider = QRangeSlider(Qt.Horizontal) slider = QRangeSlider(Qt.Horizontal) slider.setValue((20, 80)) slider.show() app.exec_()
def __init__(self) -> None: super().__init__() reg_hslider = QtW.QSlider(QtCore.Qt.Horizontal) reg_hslider.setValue(50) range_hslider = QRangeSlider(QtCore.Qt.Horizontal) range_hslider.setValue((20, 80)) multi_range_hslider = QRangeSlider(QtCore.Qt.Horizontal) multi_range_hslider.setValue((11, 33, 66, 88)) multi_range_hslider.setTickPosition(QtW.QSlider.TicksAbove) styled_reg_hslider = QtW.QSlider(QtCore.Qt.Horizontal) styled_reg_hslider.setValue(50) styled_reg_hslider.setStyleSheet(QSS) styled_range_hslider = QRangeSlider(QtCore.Qt.Horizontal) styled_range_hslider.setValue((20, 80)) styled_range_hslider.setStyleSheet(QSS) reg_vslider = QtW.QSlider(QtCore.Qt.Vertical) reg_vslider.setValue(50) range_vslider = QRangeSlider(QtCore.Qt.Vertical) range_vslider.setValue((22, 77)) tick_vslider = QtW.QSlider(QtCore.Qt.Vertical) tick_vslider.setValue(55) tick_vslider.setTickPosition(QtW.QSlider.TicksRight) range_tick_vslider = QRangeSlider(QtCore.Qt.Vertical) range_tick_vslider.setValue((22, 77)) range_tick_vslider.setTickPosition(QtW.QSlider.TicksLeft) szp = QtW.QSizePolicy.Maximum left = QtW.QWidget() left.setLayout(QtW.QVBoxLayout()) left.setContentsMargins(2, 2, 2, 2) label1 = QtW.QLabel("Regular QSlider Unstyled") label2 = QtW.QLabel("QRangeSliders Unstyled") label3 = QtW.QLabel("Styled Sliders (using same stylesheet)") label1.setSizePolicy(szp, szp) label2.setSizePolicy(szp, szp) label3.setSizePolicy(szp, szp) left.layout().addWidget(label1) left.layout().addWidget(reg_hslider) left.layout().addWidget(label2) left.layout().addWidget(range_hslider) left.layout().addWidget(multi_range_hslider) left.layout().addWidget(label3) left.layout().addWidget(styled_reg_hslider) left.layout().addWidget(styled_range_hslider) right = QtW.QWidget() right.setLayout(QtW.QHBoxLayout()) right.setContentsMargins(15, 5, 5, 0) right.layout().setSpacing(30) right.layout().addWidget(reg_vslider) right.layout().addWidget(range_vslider) right.layout().addWidget(tick_vslider) right.layout().addWidget(range_tick_vslider) self.setLayout(QtW.QHBoxLayout()) self.layout().addWidget(left) self.layout().addWidget(right) self.setGeometry(600, 300, 580, 300) self.activateWindow() self.show()
from qtrangeslider import QRangeSlider from qtrangeslider.qtcompat.QtWidgets import QApplication app = QApplication([]) slider = QRangeSlider() slider.setMinimum(0) slider.setMaximum(200) slider.setValue((0, 40, 80, 160)) slider.show() app.exec_()
def test_value(qtbot, orientation): rs = QRangeSlider(getattr(Qt, orientation)) qtbot.addWidget(rs) rs.setValue([10, 20]) assert rs.value() == (10, 20)