Skip to content

Commit

Permalink
Fix size hints (sync with QSlider)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 26, 2020
1 parent 2683a57 commit c5e0513
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsrangeslider.sip.in
Expand Up @@ -142,6 +142,8 @@ position for the widget. If the value is ``True``, the minimum and maximum appea

virtual QSize sizeHint() const;

virtual QSize minimumSizeHint() const;


public slots:

Expand Down
34 changes: 25 additions & 9 deletions src/gui/qgsrangeslider.cpp
Expand Up @@ -494,17 +494,33 @@ void QgsRangeSlider::mouseReleaseEvent( QMouseEvent *event )

QSize QgsRangeSlider::sizeHint() const
{
static constexpr int sliderLength = 84;
static constexpr int tickSpace = 5;

int w = sliderLength;
int h = style()->pixelMetric( QStyle::PM_SliderThickness, &mStyleOption, this );

if ( mStyleOption.tickPosition & QSlider::TicksAbove || mStyleOption.tickPosition & QSlider::TicksBelow )
ensurePolished();

// these hardcoded magic values look like a hack, but they are taken straight from the Qt QSlider widget code!
static constexpr int SLIDER_LENGTH = 84;
static constexpr int TICK_SPACE = 5;

int thick = style()->pixelMetric( QStyle::PM_SliderThickness, &mStyleOption, this );
if ( mStyleOption.tickPosition & QSlider::TicksAbove )
thick += TICK_SPACE;
if ( mStyleOption.tickPosition & QSlider::TicksBelow )
thick += TICK_SPACE;
int w = thick, h = SLIDER_LENGTH;
if ( mStyleOption.orientation == Qt::Horizontal )
{
h += tickSpace;
std::swap( w, h );
}

return style()->sizeFromContents( QStyle::CT_Slider, &mStyleOption, QSize( w, h ), this );
}

QSize QgsRangeSlider::minimumSizeHint() const
{
QSize s = sizeHint();
int length = style()->pixelMetric( QStyle::PM_SliderLength, &mStyleOption, this );
if ( mStyleOption.orientation == Qt::Horizontal )
s.setWidth( length );
else
s.setHeight( length );
return s;
}

1 change: 1 addition & 0 deletions src/gui/qgsrangeslider.h
Expand Up @@ -145,6 +145,7 @@ class GUI_EXPORT QgsRangeSlider : public QWidget
void mouseMoveEvent( QMouseEvent *event ) override;
void mouseReleaseEvent( QMouseEvent *event ) override;
QSize sizeHint() const override;
QSize minimumSizeHint() const override;

public slots:

Expand Down

0 comments on commit c5e0513

Please sign in to comment.