theChef613
theChef613

Reputation: 125

Qt connecting a slider to a doublespinbox

this is the code I am using to convert the int of my slider into a double.

void simMotionControl::on_horizontalSlider_speed_valueChanged(int value)
{
    value = double (value/100); //i set the range of the slider from 0 to 10000
    ui->doubleSpinBox_speed->setValue(value);
}

When I connect my slider to my double spin box, it only changes its number for every integer how can i have my slider change my double spin box to the precision of two decimal places? also, i want to also connect the spin box back to my slider so if i change the value in the spinbox the slider will change. thank you!!

Upvotes: 0

Views: 1724

Answers (1)

Nejat
Nejat

Reputation: 32635

You are dividing two integers which always results in an integer value. You should divide value by 100.0 which is decimal :

void simMotionControl::on_horizontalSlider_speed_valueChanged(int value)
{
    double val = value/100.0; //i set the range of the slider from 0 to 10000
    ui->doubleSpinBox_speed->setValue(val);
}

It is also possible to cast value to double prior to the division :

double val = (double)value/100;

For updating the value of the slider when changing spinbox you can have :

void simMotionControl::on_doubleSpinBox_speed_valueChanged(double arg1)
{
    ui->horizontalSlider_speed->setValue(arg1*100);
}

Upvotes: 1

Related Questions