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

How to customize groove of the QSlider Class dynamically based on values?



  • Hello I am new to QT. I have a QSlider that can return values from 0 to 86399 based on the position of the handle. now i want to change the colour of the groove for some range of values (between 0 - 86399) that user will input.
    For example user might input two value ranges:
    1. 450, 1900
    2. 5300, 6000
    Now based on this two value ranges, i will have to colour the parts of the Groove that lies within these two ranges. The rest of the part of the groove will be left as it is.
    I tried to reimplement the PaintEvent, but I couldnot get these value ranges to be passed as parameters for PaintEvent.
    I tried this code:

    void timeSlider::paintEvent(QPaintEvent *ev) {
        QSlider::paintEvent(ev);
    
        QStyleOptionSlider opt;
        initStyleOption(&opt);
    
    
        opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
        if (tickPosition() != NoTicks) {
          opt.subControls |= QStyle::SC_SliderTickmarks;
        }
    
        QRect groove_rect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
    
        QRect rect(groove_rect.left() + 0.2 * groove_rect.width(), groove_rect.top(), 0.6 * groove_rect.width(), groove_rect.height());
    
        QPainter painter(this);
        painter.fillRect(rect, QBrush(Qt::darkRed));
    
    }
    

    It can colour the groove , but it is static. I want something that can change based on the values the user will input.
    Can somebody give me an idea how to implement this?
    NOTE: I am familiar with setstylesheet, but doing it with setstylesheet might keep me from adding some new features to the slider. But obviously if PaintEvent doesnot work I will be needing setstylesheet. Please feel free to give me some advice on setstylesheet as well. I would really appreciate the help. :)


  • Lifetime Qt Champion

    Hi,

    Why don't you just read the value from the slider directly ?



  • well I need to integrate some features which requires the user to input the "value ranges". But that is not the issue. The problem I am facing is I am unable to coulour the groove based on the given ranges. For example if i have a value range from 5000 -6000. I want to colour the part of the groove that has values from 5000-6000!
    It would look something like this: click to see image


  • Lifetime Qt Champion

    The groove is the element you use to move with the mouse. It seems you want to paint the region underneath, don't you ?

    As for the range, it's the same as the value, you can access it from the paint event. The range is nothing more that the minimum and maximum value of the slider.



  • @SGaist oh yes. sorry i thought groove is the region underneath the "element that we use the mouse to move "



  • @SGaist I have an image of the Qslider, i want my Qslider to look like the image that i have. But why cant i upload the image of the QSlider to the forum?


  • Lifetime Qt Champion

    What error do you get ?


  • Moderators

    @dracodraco said in How to customize groove of the QSlider Class dynamically based on values?:

    why cant i upload the image of the QSlider to the forum?

    This forum can't receive image uploads.

    Upload it to an external image host and post it here using this syntax:

    ![Image description](Image link)



  • This post is deleted!

Log in to reply