Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSlider with SVG groove and knob

QSlider with SVG groove and knob

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 4 Posters 6.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.
  • G Offline
    G Offline
    Gourmet
    wrote on last edited by Gourmet
    #1

    I need sublclass QSlider but I must draw it not by pixmaps but SVG instead. Thefore customizing by stylesheets looks not apporopriate for me. Anybody who is familiar with this control - how it can be customized with SVG? What should I implement - paintEvent() for entire control with groove and knob? Or may be draw groove and knob from different SVG files separately? Or may be SVG can be used in stylesheets?

    1 Reply Last reply
    0
    • BuckwheatB Offline
      BuckwheatB Offline
      Buckwheat
      wrote on last edited by
      #2

      @Gourmet ... Of course you can override the paintEvent and handle the drawing yourself using QSvgRenderer or you can try using the style property:

      QSlider::groove {
      url(":<where you have your art>/groove.svg");
      }
      QSlider::handle {
      url(":<where you have your art>/handle.svg");
      }

      Of course, the <where you have your art> is the resource file path.

      See: http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qslider for a more detailed explanation.

      Dave Fileccia

      G 1 Reply Last reply
      0
      • BuckwheatB Buckwheat

        @Gourmet ... Of course you can override the paintEvent and handle the drawing yourself using QSvgRenderer or you can try using the style property:

        QSlider::groove {
        url(":<where you have your art>/groove.svg");
        }
        QSlider::handle {
        url(":<where you have your art>/handle.svg");
        }

        Of course, the <where you have your art> is the resource file path.

        See: http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qslider for a more detailed explanation.

        G Offline
        G Offline
        Gourmet
        wrote on last edited by
        #3

        @Buckwheat In docs there is nothing about use SVG in stylesheet. Will this really work? Ok, I'll try it first.

        mrjjM 1 Reply Last reply
        0
        • G Gourmet

          @Buckwheat In docs there is nothing about use SVG in stylesheet. Will this really work? Ok, I'll try it first.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Gourmet
          Hi
          it does work with border-image
          alt text

          1 Reply Last reply
          1
          • BuckwheatB Offline
            BuckwheatB Offline
            Buckwheat
            wrote on last edited by
            #5

            @Gourmet ... Just try. I have never done a slider but I have done others and I have used many kinds of images.

            Dave Fileccia

            G 1 Reply Last reply
            1
            • BuckwheatB Buckwheat

              @Gourmet ... Just try. I have never done a slider but I have done others and I have used many kinds of images.

              G Offline
              G Offline
              Gourmet
              wrote on last edited by Gourmet
              #6

              What about QDial? It is "slider" too but I see some say it is not possible just use stylesheet to replace image. But may be it was true for older Qt release. I use 5.6.1.

              I just do not wanna waste time and kick the wall by head if something is not possible. If anybody EXACTLY made skinned QDial and QSlider - please show me the idea how implement it. Only show the idea - I will write by myself.

              1 Reply Last reply
              0
              • BuckwheatB Offline
                BuckwheatB Offline
                Buckwheat
                wrote on last edited by
                #7

                @Gourmet ... your best bet would be my first suggestion and override the paintEvent and draw your own interface. Trying to use a style sheet takes 5 minutes so it isn't wasting time if you have never tried before. You will know immediately if it does not work. Just make sure your art is in a QRC so you can use the proper notation if you are loading the image.

                Dave Fileccia

                G 1 Reply Last reply
                0
                • BuckwheatB Buckwheat

                  @Gourmet ... your best bet would be my first suggestion and override the paintEvent and draw your own interface. Trying to use a style sheet takes 5 minutes so it isn't wasting time if you have never tried before. You will know immediately if it does not work. Just make sure your art is in a QRC so you can use the proper notation if you are loading the image.

                  G Offline
                  G Offline
                  Gourmet
                  wrote on last edited by Gourmet
                  #8

                  @Buckwheat for QDial stylesheet image replacement doesn't work. I already implemented it with paintEvent() replacement. And some other features added which are impossible by just stylesheet image replacement. This QDial with SVG in .qrc file works fine for me. Even it is Q_PROPERTY adjustable and shows graphics fine in QtDesigner form. Yes, I impemented it as promoted plugin for Designer. I suppose just copy this code for QSlider and just replace and add all needed parts. Nobody did not answer if stylesheet works fine for QSlider and I do not want waste time for dead-end experiments. This all MUST be clearly described in documentation instead of doing "try-and-fail" attempts.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    euchkatzl
                    wrote on last edited by
                    #9
                    /void Slider::paintEvent(QPaintEvent *e)
                    {
                        QSlider::paintEvent(e);
                        QPainter painter(this);
                        QStyleOptionSlider opt;
                        initStyleOption(&opt);
                        QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);    
                        QPainter* p = &painter;    
                        QRect groove = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
                        QRect drawingRect = groove;
                        qreal height = 4;
                        drawingRect.setHeight(height);
                    
                        drawingRect.moveCenter(groove.center());
                        QPainterPath path;
                        path.addRoundedRect(drawingRect,height / 2.0,height / 2.0);
                    
                        if (opt.state & QStyle::State_Enabled) {
                            p->fillPath(path,grooveColor);
                        }else {
                            p->fillPath(path,grooveColorDisabled);
                        }
                        opt.rect = handle;
                        
                    
                    }
                    

                    You can then paint your svg in "handle" Rect with http://doc.qt.io/qt-5/qsvgrenderer.html#render-1

                    1 Reply Last reply
                    2
                    • G Offline
                      G Offline
                      Gourmet
                      wrote on last edited by
                      #10

                      In QDial I have calculate rotation angle and rotate QPainter for image by myself. I only wanted know - in QSlider should I calculate position for handle and move it - or I just have to paint on handle and groove?

                      1 Reply Last reply
                      0
                      • BuckwheatB Offline
                        BuckwheatB Offline
                        Buckwheat
                        wrote on last edited by
                        #11

                        @Gourmet ... Wow! Your time must be more valuable than any one elses. I think the 5 minute test would not only be a good learning experience but you would get the answer yourself instead of relying on others for the answer. An answer discovered or earned is one that lasts a lifetime!

                        Dave Fileccia

                        G 1 Reply Last reply
                        0
                        • BuckwheatB Buckwheat

                          @Gourmet ... Wow! Your time must be more valuable than any one elses. I think the 5 minute test would not only be a good learning experience but you would get the answer yourself instead of relying on others for the answer. An answer discovered or earned is one that lasts a lifetime!

                          G Offline
                          G Offline
                          Gourmet
                          wrote on last edited by Gourmet
                          #12

                          @Buckwheat I hate "try-and-fail" learning technique but prefer learn others experience if it is possible. It is much more productive. Only fools first teach themselves on their own mistakes - smart people learn other's mistakes before action.

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13
                            This world was build from people that tried them self, 
                                              failed, learned from it 
                            and did not give up 
                                       and just tried again.
                            

                            They were not fools - they were pioneers ...

                            G 1 Reply Last reply
                            1
                            • mrjjM mrjj
                              This world was build from people that tried them self, 
                                                failed, learned from it 
                              and did not give up 
                                         and just tried again.
                              

                              They were not fools - they were pioneers ...

                              G Offline
                              G Offline
                              Gourmet
                              wrote on last edited by Gourmet
                              #14

                              @mrjj pioneers were those who were not able follow others experience.

                              Looks like nobody had used SVG on QSlider stylesheets before. Therefore I will reload paintEvent(). And upvote @euchkatzl's post as best help.

                              mrjjM 1 Reply Last reply
                              0
                              • G Gourmet

                                @mrjj pioneers were those who were not able follow others experience.

                                Looks like nobody had used SVG on QSlider stylesheets before. Therefore I will reload paintEvent(). And upvote @euchkatzl's post as best help.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #15

                                @Gourmet said in QSlider with SVG groove and knob:

                                pioneers were those who were not able follow others experience.

                                So by your logic, all pioneers are fools :)

                                G 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @Gourmet said in QSlider with SVG groove and knob:

                                  pioneers were those who were not able follow others experience.

                                  So by your logic, all pioneers are fools :)

                                  G Offline
                                  G Offline
                                  Gourmet
                                  wrote on last edited by Gourmet
                                  #16

                                  @mrjj nope, fools were those who were able follow others experience but tried and failed instead

                                  1 Reply Last reply
                                  0
                                  • BuckwheatB Offline
                                    BuckwheatB Offline
                                    Buckwheat
                                    wrote on last edited by
                                    #17

                                    @Gourmet ... Einstein was a fool... hmmm... Darwin was a fool.... hmmm... Entire NASA space field of the 60s were fools... hmmm... I am glad to be in their company! Fools are those who do not learn and grow. Followers often end up going over cliffs. I refer to them as lemmings.

                                    Dave Fileccia

                                    1 Reply Last reply
                                    0
                                    • mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      This is getting rather philosophical so im going to close it by saying that on the other hand i fully understand if using svg on some knob is just a fast task
                                      and the time needed to understand the docs that fully describe what Widgets supports what tags is wasted as this knowledge cannot be used for anything further in the project -
                                      then the concept of just using other people's experience seems reasonable.

                                      However, in that spirit, I hope you will post the paintEvent code so others, feeling like you, can
                                      benefit from your working solution and thereby promote your approach as valid. :)

                                      G 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        This is getting rather philosophical so im going to close it by saying that on the other hand i fully understand if using svg on some knob is just a fast task
                                        and the time needed to understand the docs that fully describe what Widgets supports what tags is wasted as this knowledge cannot be used for anything further in the project -
                                        then the concept of just using other people's experience seems reasonable.

                                        However, in that spirit, I hope you will post the paintEvent code so others, feeling like you, can
                                        benefit from your working solution and thereby promote your approach as valid. :)

                                        G Offline
                                        G Offline
                                        Gourmet
                                        wrote on last edited by
                                        #19

                                        @mrjj the SVG requirement is not just a whim. I need it to inedpendently set size and proportions for slider knob and groove. Result must allow resize them separetely in QtDesigner. This doesn't look too musch simple. And it cannot be defenitely made with just stylesheet - this way doesn't allow scale groove and knob separetely.

                                        mrjjM 1 Reply Last reply
                                        0
                                        • G Gourmet

                                          @mrjj the SVG requirement is not just a whim. I need it to inedpendently set size and proportions for slider knob and groove. Result must allow resize them separetely in QtDesigner. This doesn't look too musch simple. And it cannot be defenitely made with just stylesheet - this way doesn't allow scale groove and knob separetely.

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @Gourmet
                                          Nope, full design time ability will be a lot of work and i dont think you
                                          can avoid a Designer plugin if it should be previewable in Designer.

                                          G 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