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
Forum Updated to NodeBB v4.3 + New Features

Custom behavior for QSlider - tips/hints needed

Scheduled Pinned Locked Moved Solved General and Desktop
qslidermouse eventsdesktop
14 Posts 5 Posters 3.2k Views 4 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.
  • MasterBLBM MasterBLB

    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.

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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
    • MasterBLBM Offline
      MasterBLBM Offline
      MasterBLB
      wrote on last edited by
      #3

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

      raven-worxR 1 Reply Last reply
      0
      • MasterBLBM MasterBLB

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on 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
        • MasterBLBM Offline
          MasterBLBM Offline
          MasterBLB
          wrote on 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.HilkJ 1 Reply Last reply
          0
          • MasterBLBM MasterBLB

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

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on 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
            • MasterBLBM MasterBLB

              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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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
              • MasterBLBM Offline
                MasterBLBM Offline
                MasterBLB
                wrote on last edited by MasterBLB
                #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
                • MasterBLBM Offline
                  MasterBLBM Offline
                  MasterBLB
                  wrote on last edited by
                  #9

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

                  kshegunovK 1 Reply Last reply
                  0
                  • MasterBLBM MasterBLB

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

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on 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
                    • MasterBLBM Offline
                      MasterBLBM Offline
                      MasterBLB
                      wrote on 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

                      kshegunovK 1 Reply Last reply
                      0
                      • MasterBLBM MasterBLB

                        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

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on 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

                        JKSHJ 1 Reply Last reply
                        0
                        • kshegunovK kshegunov

                          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.

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on 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
                          • MasterBLBM Offline
                            MasterBLBM Offline
                            MasterBLB
                            wrote on last edited by
                            #14

                            Thank you @JKSH .

                            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