如何将 QSlider 连接到 QDoubleSpinBox
我想将 QSlider 连接到 QDoubleSpinBox,但是虽然代码编译良好并可以运行简单的 QSpinBox,但它不适用于 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)) );
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)) );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
QSlider 和 QDoubleSpinBox 在
valueChanged
/setValue
中采用不同类型的参数(当然,QSlider 使用整数,QDoubleSpinBox 使用双精度)。更改滑块信号和槽的参数类型可能会有所帮助:我不确定 Qt 是否可以自动为您处理这种类型转换;如果没有,您必须定义自己的插槽才能在正确的对象上调用 setValue()
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: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
您必须添加自己的插槽来转换参数类型并发出信号或直接更新滑块。
You'll have to add your own slot which converts the argument type and emits a signal or updates the slider directly.
正如 Dave Kilian 回答的那样,QSlider 的信号和插槽不使用双精度。另外,Qt 4 不会自动转换类型;它不会允许您连接它们。因此,您必须编写自己的转换槽来更新其他项目的值。
As Dave Kilian answered, the signals and slots for QSlider don't use double. Also, Qt 4 does not automatically convert the types; it will not allow you to connect them. So, you will have to write your own conversion slot which updates the value of the other item.
希望这可以帮助你。
Hope this could help you.
将 QSlider 或 QDial 同步到 QDoubleSpinBox 的一个问题是,当 QDoubleSpinBox 更新 QDial 或 QSlider 时,QDial 或 QSlider valueChanged 信号将被触发,导致它们更新 QDoubleSpinBox 作为回报。这会导致您无法在 QDoubleSpinBox 中输入小数值的问题,因为当您输入小数点“.”时,QDial/Slider 更新会覆盖小数点,从而阻止您输入小数点值的其余部分。
要解决此问题,请让每个输入小部件检查它们的值是否已经与它们正在写入的小部件中的值匹配,如果匹配,则不要更新该值。下面是一个允许 QDial 和 QDoubleSpinBox 相互更新的函数示例:
在此示例中,QDoubleSpinBox 是百分比 (0.0-100.0),QDial 有 100,000 步支持百分比的浮点输出,精确到小数点后三位 ( 0.000% 至 100.000%)。
One catch to syncing a QSlider or QDial to a QDoubleSpinBox is that when the QDoubleSpinBox updates the QDial or QSlider, the QDial or QSlider valueChanged signal will be triggered causing them to update the QDoubleSpinBox in return. This causes an issue where you cannot enter decimal values into the QDoubleSpinBox, because when you enter a decimal ".", the QDial/Slider update overwrites the decimal, blocking you from entering the rest of the decimal value.
To fix this, have each input widget check if their value already matches the value in the widget they are writing to, and if they do, then do not update the value. Here is an example of a function that allows a QDial and QDoubleSpinBox to update each other:
In this example, the QDoubleSpinBox is a percentage (0.0-100.0) and the QDial has 100,000 steps to support a float output of percentages to three decimal places (0.000% to 100.000%).
在这个非常相似的问题中有一些关于如何创建自己的插槽并发出信号的示例代码:
使用创建的向量作为 QDoubleSpinBox 和 QSlider 的范围
There is some example code on how to create your own slots and emit signals in this very similar question:
use a created vector as range for QDoubleSpinBox and QSlider