Customizing a QSlider in a style subclass



  • Hello,

    I am making a custom style subclassing from one of Qt's style classes (QMacStyle) and I am trying to first of all, make the sliders that I use different. For this I want to change the slider handle to be an image I have to give it a different look.

    What I have tried so far is doing something like this:

    @
    void CustomStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter p, const QWidget w) const
    {
    if(cc == CC_Slider)
    {
    const QSlider
    slider = static_cast< const QSlider
    >(w);
    if(slider->orientation() == Qt::Horizontal){
    drawHSlider(SC_SliderHandle, opt, p);
    }
    }

    else
        return QMacStyle::drawComplexControl(cc, opt, p, w);
    

    }

    ...

    void CustomStyle::drawHSlider(SubControl handle, const QStyleOptionComplex *option, QPainter *painter) const
    {
    if(handle == SC_SliderHandle){
    QPixmap fader(":/images/hfader");
    QRect handleRect = subControlRect(CC_Slider, option, handle);

        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        painter->drawPixmap(handleRect, fader);
        painter->restore();
    }
    

    }
    @

    But I'm kinda lost on how to draw the image to the handle of the slider and be able to move the slider like usual. Any help I would really appreciate. Thanks!



  • Wouldn't it be easier to use a style sheet instead? Perhaps something like:

    @
    QSlider::handle {image:url(:/images/hfacer) }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.