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. Custom behavior for QSlider - tips/hints needed

Custom behavior for QSlider - tips/hints needed

Scheduled Pinned Locked Moved Solved General and Desktop
qslidermouse eventsdesktop
14 Posts 5 Posters 3.1k 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.
  • M Offline
    M Offline
    MasterBLB
    wrote on 30 Nov 2018, 08:20 last edited by
    #1

    Hey mates

    I'd like to change behavior of QSlider - to forbid other middle values than step(), ex min = 0, max = 10, step = 2, so the slider may get only values 0, 2, 4, 6, 8, 10.
    Unfortunately, dragging slider by mouse, or pointing a point on a slider and holding LMB causes the slider to set any value in range <min, max>. Any hints how I could change that assuming I'd like to preserve as much original mouseXXXEvent() as possible?I don't mind reimplementing these functions, though.

    R J 2 Replies Last reply 30 Nov 2018, 08:39
    0
    • M MasterBLB
      30 Nov 2018, 09:20

      That was the 1st I've tried (real used values are range <0, 100>, step 5) but it's still possible to set slider value to say 67 by clicking LMB on slider handle, and move it via mouse move.
      And I'd like the handle to stay in place while being dragged via mouse, and be moved only if the new value would be multiplication of step.

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 30 Nov 2018, 10:00 last edited by
      #7

      @MasterBLB
      in that cause.

      A quick and dirty workaround could be:

      QSlider *slider(new QSlider);
          slider->setRange(0,100);
          slider->show();
      
          QObject::connect(slider, &QSlider::valueChanged, slider, [slider](int value){
              const int step = 5;
              int offset = value % step;
              if( offset != 0)
                  slider->setValue(value - offset);
          });
      

      use at one's own risk


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • M MasterBLB
        30 Nov 2018, 08:20

        Hey mates

        I'd like to change behavior of QSlider - to forbid other middle values than step(), ex min = 0, max = 10, step = 2, so the slider may get only values 0, 2, 4, 6, 8, 10.
        Unfortunately, dragging slider by mouse, or pointing a point on a slider and holding LMB causes the slider to set any value in range <min, max>. Any hints how I could change that assuming I'd like to preserve as much original mouseXXXEvent() as possible?I don't mind reimplementing these functions, though.

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 30 Nov 2018, 08:39 last edited by
        #2

        @MasterBLB
        easiest would be

        1. set the range of the slider to <0, step-count>
        2. whenever you need to process the value calculate it: min + value * stepSize

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MasterBLB
          wrote on 30 Nov 2018, 08:46 last edited by
          #3

          Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?

          R 1 Reply Last reply 30 Nov 2018, 09:41
          0
          • M MasterBLB
            30 Nov 2018, 08:20

            Hey mates

            I'd like to change behavior of QSlider - to forbid other middle values than step(), ex min = 0, max = 10, step = 2, so the slider may get only values 0, 2, 4, 6, 8, 10.
            Unfortunately, dragging slider by mouse, or pointing a point on a slider and holding LMB causes the slider to set any value in range <min, max>. Any hints how I could change that assuming I'd like to preserve as much original mouseXXXEvent() as possible?I don't mind reimplementing these functions, though.

            J Online
            J Online
            J.Hilk
            Moderators
            wrote on 30 Nov 2018, 09:14 last edited by
            #4

            @MasterBLB
            Maybe I'm completely off here, but QSlider has 2 Settings/Properties that handle the step range

            singleStep and pageStep

            can't you simply set both to your desiered setp-value?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MasterBLB
              wrote on 30 Nov 2018, 09:20 last edited by
              #5

              That was the 1st I've tried (real used values are range <0, 100>, step 5) but it's still possible to set slider value to say 67 by clicking LMB on slider handle, and move it via mouse move.
              And I'd like the handle to stay in place while being dragged via mouse, and be moved only if the new value would be multiplication of step.

              J 1 Reply Last reply 30 Nov 2018, 10:00
              0
              • M MasterBLB
                30 Nov 2018, 08:46

                Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 30 Nov 2018, 09:41 last edited by raven-worx
                #6

                @MasterBLB said in Custom behavior for QSlider - tips/hints needed:

                Hmmm...do you have void QAbstractSlider::sliderChange(QAbstractSlider::SliderChange change) in mind @raven-worx ?

                no.
                my suggestion doesn't require any change/subclassing of QSlider. Just how you interpret the returned value

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • M MasterBLB
                  30 Nov 2018, 09:20

                  That was the 1st I've tried (real used values are range <0, 100>, step 5) but it's still possible to set slider value to say 67 by clicking LMB on slider handle, and move it via mouse move.
                  And I'd like the handle to stay in place while being dragged via mouse, and be moved only if the new value would be multiplication of step.

                  J Online
                  J Online
                  J.Hilk
                  Moderators
                  wrote on 30 Nov 2018, 10:00 last edited by
                  #7

                  @MasterBLB
                  in that cause.

                  A quick and dirty workaround could be:

                  QSlider *slider(new QSlider);
                      slider->setRange(0,100);
                      slider->show();
                  
                      QObject::connect(slider, &QSlider::valueChanged, slider, [slider](int value){
                          const int step = 5;
                          int offset = value % step;
                          if( offset != 0)
                              slider->setValue(value - offset);
                      });
                  

                  use at one's own risk


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    MasterBLB
                    wrote on 1 Dec 2018, 09:59 last edited by MasterBLB 12 Jan 2018, 10:20
                    #8

                    Thanks @J-Hilk , the last workaround works almost exactly as I'd like it to - however, middle values are still sent via signal valueChanged() :/ though I think I could add logic into receiver to ignore everything which is not sliderValue % sliderStep() = 0

                    EDIT:
                    SPLENDID, the combined approach worked! Thank you very much Brother.

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      MasterBLB
                      wrote on 1 Dec 2018, 10:22 last edited by
                      #9

                      WTF, why I can't set J.Hilk's post as correct answer?? o.O Moderators, please help!

                      K 1 Reply Last reply 1 Dec 2018, 10:40
                      0
                      • M MasterBLB
                        1 Dec 2018, 10:22

                        WTF, why I can't set J.Hilk's post as correct answer?? o.O Moderators, please help!

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 1 Dec 2018, 10:40 last edited by
                        #10

                        Should be able to. What's not working with it? Did you find the button to do so?

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          MasterBLB
                          wrote on 1 Dec 2018, 10:58 last edited by
                          #11

                          I'm poining those 3 dots with mouse, but in the menu there is no option "mark this post as correct answer". I have the option for my posts only o.O

                          K 1 Reply Last reply 1 Dec 2018, 11:29
                          0
                          • M MasterBLB
                            1 Dec 2018, 10:58

                            I'm poining those 3 dots with mouse, but in the menu there is no option "mark this post as correct answer". I have the option for my posts only o.O

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 1 Dec 2018, 11:29 last edited by
                            #12

                            That's odd, it may be a bug in the forum software. I can see it for all posts, but it should be available to you too. I'll make inquiries.

                            Read and abide by the Qt Code of Conduct

                            J 1 Reply Last reply 1 Dec 2018, 11:59
                            0
                            • K kshegunov
                              1 Dec 2018, 11:29

                              That's odd, it may be a bug in the forum software. I can see it for all posts, but it should be available to you too. I'll make inquiries.

                              J Offline
                              J Offline
                              JKSH
                              Moderators
                              wrote on 1 Dec 2018, 11:59 last edited by
                              #13

                              @kshegunov said in Custom behavior for QSlider - tips/hints needed:

                              That's odd, it may be a bug in the forum software. I can see it for all posts, but it should be available to you too. I'll make inquiries.

                              Thanks, @kshegunov. The inquiry is at https://forum.qt.io/topic/97183/mark-as-correct-answer-not-appearing-for-some-posts

                              @MasterBLB, I've marked the solution for you in the meantime.

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

                              1 Reply Last reply
                              3
                              • M Offline
                                M Offline
                                MasterBLB
                                wrote on 1 Dec 2018, 19:47 last edited by
                                #14

                                Thank you @JKSH .

                                1 Reply Last reply
                                0

                                9/14

                                1 Dec 2018, 10:22

                                • Login

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