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. Unable to set the intermediate / float values to a QSlider

Unable to set the intermediate / float values to a QSlider

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.3k 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.
  • G Offline
    G Offline
    gpkk
    wrote on last edited by
    #1

    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.

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • G gpkk

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        4
        • G Offline
          G Offline
          gpkk
          wrote on last edited by
          #4

          @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);
          
          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • G gpkk

              @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);
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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
              0
              • G gpkk

                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.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @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
                0
                • G Offline
                  G Offline
                  gpkk
                  wrote on last edited by
                  #8

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

                  Pl45m4P 1 Reply Last reply
                  0
                  • G gpkk

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

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    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
                    2

                    • Login

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