Unable to set the intermediate / float values to a QSlider

  • 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,

    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.

  • @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.

  • Lifetime Qt Champion

    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)

  • @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.

    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;
        for(int i=1; i<25;++i)
            vecValues.push_back(static_cast<int>( std::pow( 2.0, i ) ));
        setRange(2, vecValues.size() -1);

  • Lifetime Qt Champion

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

  • @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.

  • @gpkk

    The Qwt slider can :)


    The Qt QML version of QSlider also can

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

  • @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... :(

  • 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

Log in to reply