QSlider with SVG groove and knob
-
@mrjj I did not tell I will avoid plugin. I created plugin later and it works fine including Q_PROPERTIES support. It is not a problem, I have created several other plugins before. They even are compiled and work as plugins on Linux workstation but they are compiled and work as just shared objects on Android tablet.
Now I need only create proper paintEvent(). But I still cannot get fine result. Code
void SSwidget::paintEvent( QPaintEvent* ) { QPainter painter( this ); painter.setRenderHint( QPainter::Antialiasing, true ); QStyleOptionSlider o; initStyleOption(&o); QRect knobRect = style()->subControlRect(QStyle::CC_Slider, &o, QStyle::SC_SliderHandle, this); QRect grooveRect = style()->subControlRect(QStyle::CC_Slider, &o, QStyle::SC_SliderGroove, this); if( grooveSVG != Q_NULLPTR ) grooveSVG->render( &painter, grooveRect ); if( knobSVG != Q_NULLPTR ) knobSVG->render( &painter, knobRect ); }
Works almost as needed. But groove size has same length as entire widget geometry has. And knob's center moves from most left and right groove edges. In most right and left positions half of knob becomes invisible outside widget rectangle. It is needed not just only shrink groove or stretch widget's rectangle. It is also needed set knob stop points closer to each other like original slider has. If I call QSlider::paintEvent(e) - then I see oroginal slider behind mine. And it moves properly.
-
-
Finaly I did it. I had explored source code of QStyle, QWindowsXPStyle, QFusionStyle, QAndroidStyle, QCommonStyle and some other classes. Solution is - before
QRect knobRect = style()->subControlRect(QStyle::CC_Slider, &o, QStyle::SC_SliderHandle, this); QRect grooveRect = style()->subControlRect(QStyle::CC_Slider, &o, QStyle::SC_SliderGroove, this);
it is needed change size and position of o.rect but after initStyleOption(&o). Call of subControlRect() will calculate new position of knob properly depending from minimum and maximum slider value in margins of o.rect. This solution is much more powerful and flexible than via stylesheet.