简体   繁体   中英

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!!

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM