Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSlider in Windows Volume Mixer Style



  • Hi guys,

    Does someone have a cool solution to creating a Windows Volume Mixer Style QSlider?

    0_1540052841895_f62bccc8-f210-4b18-94fb-5929f66158ee-image.png

    It would be nice if it's something I can simply copy into the QtCreator stylesheet field.

    Thanks in Advance,
    Megamouse

    Edit: It doesn't have to be exactly the same. Just the triangular background would be enough already



  • @Megamouse
    you just need to subclass QSlider, and override its paintEvent() and draw the triangle-like shape within it.


  • Moderators

    @Megamouse
    I don't think that with stylesheets only this is achievable on QSlider (directly).

    I really don't know how it will look like, but generally you can do something like this:

    void MySlider::paintEvent(QPaintEvent *)
    {
        QPainter p(this);
        QStyleOptionSlider opt;
        initStyleOption(&opt);
    
        // draw rectangle
        QPainterPath path;
           path.moveTo( opt.rect.bottomLeft() );
           path.lineTo( opt.rect.topRight() );
           path.lineTo( opt.rect.topLeft() );
           path.lineTo( opt.rect.bottomLeft() );
           path.closeSubpath();
       p.saveState();
          p.setPen( Qt::NoPen );
          p.setBrush( QBrush(Qt::lightGray) );
          p.drawPath(path);
       p.restoreState();
    
        // draw slider
        opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
        if (d->tickPosition != NoTicks)
            opt.subControls |= QStyle::SC_SliderTickmarks;
        if ( isSliderDown() ) {
            opt.activeSubControls = isSliderDown();
            opt.state |= QStyle::State_Sunken;
        } else {
            opt.activeSubControls = d->hoverControl;
        }
    
        style()->drawComplexControl(QStyle::CC_Slider, &opt, &p, this);
    }
    

    Maybe the triangle coordinates need some more adjustments.


Log in to reply