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. Subclassing QSlider with handle as a Widget
Qt 6.11 is out! See what's new in the release blog

Subclassing QSlider with handle as a Widget

Scheduled Pinned Locked Moved General and Desktop
25 Posts 3 Posters 18.8k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    cincirin
    wrote on last edited by
    #3

    Yes, is perfectly doable by subclassing QSlider.
    See my slider implementation for example :
    !http://s22.postimg.org/4c9p4p0qp/Slider.png(my Slider)!

    The two green areas are two QLabels

    edit: Andre was faster :-)

    1 Reply Last reply
    0
    • BlackMambaB Offline
      BlackMambaB Offline
      BlackMamba
      wrote on last edited by
      #4

      Thank you very much for your answers.
      I want to have the behaviour of a slider I just want the handle to be a rectangle widget to draw whatever I want inside.

      Looks like your example is perfect cincirin, could you please tell me with 2 lines of codes how you acheieved that?
      Can you move these two QLabels?
      I cant figure how to access the handle by the QSlider class ....

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #5

        Could you tell us what you want to achieve? What would you like to put on the handle? If it is just a label, it might be easier to render that content yourself in your QSlider subclass.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #6

          bq. could you please tell me with 2 lines of codes how you acheieved that?

          Of course I can, but as Andre said is better to tell us what are you trying to achieve and what are your attempts to implement them ?

          bq. Can you move these two QLabels?

          Yes any widget can be moved, just set the widget position depending on your requirements.

          So in short here is my implementation:

            • subclass QSlider and create two labels in constructor having parent this slider
            • in slider resizeEvent, move the two labels in respect with new slider width
            • for the two labels install event filter ON slider object ( or you can subclass labels to implement mouse press/move/release )
            • in slider event filter I check for mouse press/move/release for the two labels and move them accordingly
          1 Reply Last reply
          0
          • BlackMambaB Offline
            BlackMambaB Offline
            BlackMamba
            wrote on last edited by
            #7

            Hello,

            I really appreciate your help.
            I want to draw an audio sinus wave in a SinusWidget to be positonned on a time line widget.
            This SinusWidget will be able to move horizontally from left to right depending on when you want the sinus wave to be played.
            I should also have events connected to the SinusWidget to be able to change its width.

            Because of this, I was thinking about a slider to provide embedded SinusWidget via the handle.

            Something like this.

            initial example :
            ISINUS WIDGETI-----------------------------------------

            position changed:
            -----------------------------ISINUS WIDGETI------------

            width changed:
            ----------------ISINUS ________ WIDGETI-----------------

            What do you think about it?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #8

              I'm sorry but I don't understand exactly what is your requirement.
              In my example I really needed a slider with three values and signals when one of the three value is changed.
              From what I understand you only need a "widget" which is moved horizontally from left to right. Is this correct ? If yes, you can use slider directly and either use style sheets or implement a custom style to draw/paint slider "widget" value.

              1 Reply Last reply
              0
              • BlackMambaB Offline
                BlackMambaB Offline
                BlackMamba
                wrote on last edited by
                #9

                Cincirin,

                Yes I need to move a widget from left to right within a certain range.
                I had the idea of a slider because it looks convenient for what I want to achieve ;) (left to right implemented, start and end positions, etc ...)
                I am sorry if I explained it in the wrong way.

                [quote author="cincirin" date="1391516376"]If yes, you can use slider directly and either use style sheets or implement a custom style to draw/paint slider “widget” value.[/quote]

                Here is my issue, I don't know how to paint the widget on the slider's handle. I don't think stylesheet can achieve that does it?
                And for custom style, how to access the handle.

                The normal way to go if the slider's handle was a class would be to subclass it and reimplement the paint method.
                I am not familiar with QStyle at all, it's probably the reason why I don't know how to do this ....

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cincirin
                  wrote on last edited by
                  #10

                  "Customizing QSlider":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qslider

                  See how to customize the slider groove and slider handle

                  1 Reply Last reply
                  0
                  • BlackMambaB Offline
                    BlackMambaB Offline
                    BlackMamba
                    wrote on last edited by
                    #11

                    [quote author="cincirin" date="1391517392"]"Customizing QSlider":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qslider

                    See how to customize the slider groove and slider handle[/quote]

                    :)

                    Cincirin, I know about this already :)
                    But this is just to customize the appearance of the handle, not transforming it to a custom widget on which you can add events, etc ...
                    Or am I mistaken?

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      cincirin
                      wrote on last edited by
                      #12

                      Ok, what events do you need ?
                      Isn't the default "signals":http://qt-project.org/doc/qt-5/qabstractslider.html#signals what you need ?

                      1 Reply Last reply
                      0
                      • BlackMambaB Offline
                        BlackMambaB Offline
                        BlackMamba
                        wrote on last edited by
                        #13

                        These events are enough yes, but I need them on the slider's handle, not on the slider itself ;)

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          cincirin
                          wrote on last edited by
                          #14

                          bq. I need them on the slider’s handle

                          Which one exactly ?
                          Basically you can subclass slider, obtain a rect of the handle style()->subControlRect and from here you can filter all mouse events from handle only. For paint of course you can reimplement paintEvent, but with style sheet is easily. What other events do you need ?

                          1 Reply Last reply
                          0
                          • BlackMambaB Offline
                            BlackMambaB Offline
                            BlackMamba
                            wrote on last edited by
                            #15

                            I need these ones on the handle:
                            virtual void mouseDoubleClickEvent(QMouseEvent * event)
                            virtual void mouseMoveEvent(QMouseEvent * event)
                            virtual void mousePressEvent(QMouseEvent * event)
                            virtual void mouseReleaseEvent(QMouseEvent * event)
                            virtual void moveEvent(QMoveEvent * event)

                            If I understand you, you want to "render" the CustomWidget without embedding it to the handle and filter the events on the rectangle zone and not on the customwidget?

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              cincirin
                              wrote on last edited by
                              #16

                              Yes you can reimplement all of these events in QSlider subclass, and filter all mouse events for handle rect only. Thats all ...
                              @
                              if (style()->subControlRect(etc).contains(event->pos())
                              @

                              1 Reply Last reply
                              0
                              • BlackMambaB Offline
                                BlackMambaB Offline
                                BlackMamba
                                wrote on last edited by
                                #17

                                ok :)

                                And for painting the customwidget, you just instantiate it into the paint method of the subclasses slider and parent it to the subclassed slider?

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  cincirin
                                  wrote on last edited by
                                  #18

                                  In my example with yes, but I needed three handles not one. With only one handle you can use QSlider signals, events and style(sheets) without any other additional widget(s)

                                  1 Reply Last reply
                                  0
                                  • BlackMambaB Offline
                                    BlackMambaB Offline
                                    BlackMamba
                                    wrote on last edited by
                                    #19

                                    And how to you render the customWidget with stylesheet?

                                    1 Reply Last reply
                                    0
                                    • C Offline
                                      C Offline
                                      cincirin
                                      wrote on last edited by
                                      #20

                                      I'm not using stylesheet in example posted. The default slider handle is rendering by application style and the other two are QLabels with image pixmap.

                                      1 Reply Last reply
                                      0
                                      • BlackMambaB Offline
                                        BlackMambaB Offline
                                        BlackMamba
                                        wrote on last edited by
                                        #21

                                        Yes it was as I thought. So how do I do ? :D

                                        1 Reply Last reply
                                        0
                                        • C Offline
                                          C Offline
                                          cincirin
                                          wrote on last edited by
                                          #22

                                          What's wrong by subclassing QSlider and reimplement all mouse events you need ? Also you can paint the slider handle either in paint event or using stylesheet. So you have a complete control on slider handle.

                                          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