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. [Solved] Communication HorziontalSlider and DoubleSpinBox Problems
Qt 6.11 is out! See what's new in the release blog

[Solved] Communication HorziontalSlider and DoubleSpinBox Problems

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,
    for the communication with horitzontalslider (int) and doublespinbox (double), following code is my solution:

    @
    void MainWindow::on_horizontalSlider_valueChanged(int value)
    {
    double n = value;
    n = n / 100;
    ui->doubleSpinBox->setValue(n);
    }

    void MainWindow::on_doubleSpinBox_valueChanged(double arg1)
    {

    int n = arg1 * 100;
    ui->horizontalSlider->setValue(n);
    

    }
    @

    The range of my doublespinbox is from 1,00 to 2,00.

    Now my Problem:
    it works from 1,00 to 1,12 then it jumps to 1,26 and so on...
    So i am not able to choose for example 1,15 but i dont know why :-(

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Let me answer this way:
      @
      qDebug() << int(1.12 * 100) << int(1.13 * 100) << int(1.14 * 100);
      @
      What do you think gets printed?

      Explicit conversion of double to int is a round-down operation and 1.13 * 100 just so happens to be represented as something like 112.9999999999 so it gets rounded down to 112 and that triggers a loop in your value changes (print the values before and after conversion and you'll see).
      If you want to round something to the nearest integer do it explicitly, eg. using std::round or qRound.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Very good answer. Thank you!
        I did it with qRound :-)

        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