[Solved] Communication HorziontalSlider and DoubleSpinBox Problems



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


  • Moderators

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.