QSliders are displayed differently on different platforms. How can I get the width
and height
of the handle
of a QSlider
, being it styled using CSS or not?
I am only aware of methods to retrieve the size
of the whole QWidget, but not the handle
?
Asked
Active
Viewed 1,472 times
2

KernelPanic
- 2,328
- 7
- 47
- 90

t0bias
- 93
- 3
- 13
2 Answers
1
There is a terrible way, which may not have to work correctly, but it still works:
QSize sliderHandleSize( const QSlider& slider )
{
const QSize _minSize = slider.minimumSizeHint();
switch( slider.orientation() )
{
case Qt::Orientation::Horizontal:
return QSize( _minSize.width() - 1, _minSize.height() );
case Qt::Orientation::Vertical:
return QSize( _minSize.width(), _minSize.height() - 1 );
default:
Q_ASSERT_X( false, "sliderHandleSize", "Today is a bad day" );
}
return _minSize;
}

ZolotovPavel
- 138
- 6
-1
Check QSlider Stylesheet example:
QSlider::groove:horizontal
{
border: 1px solid #bbb;
background: white;
height: 10px;
border-radius: 4px;
}
QSlider::sub-page:horizontal
{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #66e, stop: 1 #bbf);
background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,
stop: 0 #bbf, stop: 1 #55f);
border: 1px solid #777;
height: 10px;
border-radius: 4px;
}
QSlider::add-page:horizontal
{
background: #fff;
border: 1px solid #777;
height: 10px;
border-radius: 4px;
}
QSlider::handle:horizontal
{
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #eee, stop:1 #ccc);
border: 1px solid #777;
width: 13px;
margin-top: -2px;
margin-bottom: -2px;
border-radius: 4px;
}
QSlider::handle:horizontal:hover
{
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #fff, stop:1 #ddd);
border: 1px solid #444;
border-radius: 4px;
}
QSlider::sub-page:horizontal:disabled
{
background: #bbb;
border-color: #999;
}
QSlider::add-page:horizontal:disabled
{
background: #eee;
border-color: #999;
}
QSlider::handle:horizontal:disabled
{
background: #eee;
border: 1px solid #aaa;
border-radius: 4px;
}

KernelPanic
- 2,328
- 7
- 47
- 90

Edward
- 149
- 7
-
2Thanks for your reply Edward, however I don't see how I would get the Dimensions of the Handles from the styling Example? I am not looking for how to **set** the `width` and `heigth` but for how to **get** these values? – t0bias Jan 11 '17 at 10:49