简体   繁体   English

Qt将滑块连接到doublespinbox

[英]Qt connecting a slider to a doublespinbox

this is the code I am using to convert the int of my slider into a double. 这是我用来将滑块的int转换为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 : 您应该将value除以100.0 ,即十进制:

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 : 要在更改Spinbox时更新滑块的值,可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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