Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Unable to set the intermediate / float values to a QSlider

    General and Desktop
    4
    9
    277
    Loading More Posts
    • 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
      gpkk last edited by

      I have a QSpinbox and a QSlider in a Qwidget. I have to set the slider steps to the power of 2 and show the intervals/steps at power of 2 values. When I try to set the value from slider it should move to the pow(2) steps and the spinbox is updated accordingly, but when I set the value from QSpinbox, the slider position could be any value(not necessarily power of 2 value).

      I have tried setting the steps to power of 2 and it works fine( by setting the range(0,25) and set the setPageStep(1)),
      and calculate the 2^x internally when the value is changed from the slider to update the spinbox.
      However I am not able to set any intermediate value from the QSpinbox to the slider.
      I calculate the 2^x internally when the value is changed from the slider to update the spinbox
      Say for example spinboxValue = 5, then the slider should be moved slightly more than value 4. The image below should illustrate this correctly,
      Slider.PNG

      So here I am unable to set the slider to the values that are in between these steps.
      Please suggest if there is a way to do this.

      JonB Pl45m4 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @gpkk last edited by

        @gpkk
        I only know that QSlider::setValue() lets you set to intermediate (integer) values, not just a multiple of QSlider::singleStep(), if that is your issue.

        1 Reply Last reply Reply Quote 1
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          QSlider has fixed values. If you need some intermediate values you have to increase the range and calculate the rest by yourself as already discussed (you deleted the post for this where I already wrote this some hours ago)

          Qt has to stay free or it will die.

          1 Reply Last reply Reply Quote 4
          • G
            gpkk last edited by

            @Christian-Ehrlicher,
            If I increase the range the problem is the the tick position will also be changed and I need the tick positions at 2^x intervals.

            @JonB,
            Say I need to set the value = 3 i.e in between 2 and 4 as shown in image
            I try to calculate the exponential value, 2^x = 3, so log 3 / log 2 , so the x= 1.6, how can I set value =1.6, now I can set only integer value, so I end up setting 1.

               // This is how I set the range for the slider
                QVector<int> vecValues;
                setTickPosition(QSlider::TicksBelow);
                setPageStep(1);
            
                for(int i=1; i<25;++i)
                {
                    vecValues.push_back(static_cast<int>( std::pow( 2.0, i ) ));
                }
                setRange(2, vecValues.size() -1);
            
            JonB 1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              You can modify the tick interval with https://doc.qt.io/qt-5/qslider.html#tickInterval-prop

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 1
              • JonB
                JonB @gpkk last edited by

                @gpkk said in Unable to set the intermediate / float values to a QSlider:

                how can I set value =1.6,

                You can't, you can only widen the range to make it integer-able. And set the tick interval as per @Christian-Ehrlicher. That's all I know.

                1 Reply Last reply Reply Quote 0
                • Pl45m4
                  Pl45m4 @gpkk last edited by Pl45m4

                  @gpkk

                  The Qwt slider can :)

                  https://qwt.sourceforge.io/class_qwt_slider.html

                  The Qt QML version of QSlider also can
                  https://doc.qt.io/qt-5/qml-qtquick-controls2-slider.html

                  I'm wondering why the QWidget slider can't handle floats
                  Maybe in the future... :)


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply Reply Quote 0
                  • G
                    gpkk last edited by

                    @Christian-Ehrlicher,

                    setTickInterval() would set the intervals but the slider should also move in steps of 2^x, like when we press arrow Up /arrow down, which does not seems to work, may be I need to override the mouse event and set the value to the slider?

                    @Pl45m4, I cannot use qwt in my project... :(

                    Pl45m4 1 Reply Last reply Reply Quote 0
                    • Pl45m4
                      Pl45m4 @gpkk last edited by Pl45m4

                      For a dynamic tickInterval you have to subclass and create your own slider. The QSlider has a constant tickInterval and can't jump from 1 -> 2 -> 4 -> 8 -> 16 ... out of the box.

                      The thing is, that the QSliderhas an equal distribution of ticks, so you can't have a range from e.g. 2 to 128 with tick marks at the correct 2^x positions and make the slider also know where (2^x) - 3 is, according to the current tickInterval

                      @gpkk said in Unable to set the intermediate / float values to a QSlider:

                      press arrow Up /arrow down

                      Arrow_Key Up / Down performs a singleStep


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply Reply Quote 2
                      • First post
                        Last post