问题描述
我想将QSlider连接到QDoubleSpinBox,但是虽然代码可以很好地编译并可以在简单的QSpinBox上运行,但它不适用于QDoubleSpinBox
I want to connect a QSlider to a QDoubleSpinBox but while the code compiles fine and runs for simple QSpinBox, it doesn't work for QDoubleSpinBox
QSlider *horizontalSlider1 = new QSlider();
QDoubleSpinBox *spinBox1 = new QDoubleSpinBox();
connect(spinBox1, SIGNAL(valueChanged(double)),horizontalSlider1,SLOT(setValue(double)) );
connect(horizontalSlider1,SIGNAL(valueChanged(double)),spinBox1,SLOT(setValue(double)) );
推荐答案
QSlider和QDoubleSpinBox在<$中采用不同类型的参数c $ c> valueChanged / setValue
(当然,QSlider使用整数,而QDoubleSpinBox使用双精度)。更改滑块信号和插槽的参数类型可能会有所帮助:
QSlider and QDoubleSpinBox take different types of arguments in valueChanged
/setValue
(QSlider uses ints and QDoubleSpinBox uses doubles, of course). Changing the argument type for the slider's signal and slot might help:
connect(spinBox1, SIGNAL(valueChanged(double)),horizontalSlider1,SLOT(setValue(int)) );
connect(horizontalSlider1,SIGNAL(valueChanged(int)),spinBox1,SLOT(setValue(double)) );
我不确定Qt是否可以为您自动处理此类型转换;如果没有,则必须定义自己的插槽以在正确的对象上调用setValue()
I'm not sure if Qt can automatically handle this type conversion for you; if not, you'll have to define your own slots to call setValue() on the correct object
这篇关于如何将QSlider连接到QDoubleSpinBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!