Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. How to customize groove of the QSlider Class dynamically based on values?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved Brainstorm
10 Posts 5 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dracodraco
    wrote on last edited by
    #1

    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. :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dracodraco
        wrote on last edited by dracodraco
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 2 Replies Last reply
          0
          • SGaistS SGaist

            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.

            D Offline
            D Offline
            dracodraco
            wrote on last edited by dracodraco
            #5

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

            1 Reply Last reply
            0
            • SGaistS SGaist

              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.

              D Offline
              D Offline
              dracodraco
              wrote on last edited by
              #6

              @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?

              JKSHJ 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                What error do you get ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D dracodraco

                  @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?

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @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)

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      Ninja turtle
                      Banned
                      wrote on last edited by Ninja turtle
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved