Qt中的拨动开关 [英] Toggle Switch in Qt

查看:324
本文介绍了Qt中的拨动开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用与Qt中的 Android开关等效的元素。我在QML中找到了ToggleSwitch,但是在实际的C ++ Qt库中却找不到任何东西。我只是想丢失一些东西,还是必须自己重新实现此小部件?

I am trying to use an element which is the equivalent of Android Switches in Qt. I have found a ToggleSwitch in QML, but nothing in the actual C++ Qt libs. Am I just missing something or will I have to reimplement this widget myself?

推荐答案

@piccy的建议是我所做的事情这样的拨动开关。有一些调整。

@piccy's suggestion is what I've done for such a toggle switch previously. With a few tweaks tho.

我们必须模拟类似于iOS开/关开关的行为。这意味着您需要一个渐进的运动,而没有外部动画的情况下,滑块的极限是0-1,这是您无法做到的。

We had to emulate the behaviours similar to the iOS on/off switches. Meaning you needed a gradual movement which you won't have with slider being with a limit of 0-1 without external animations.

因此,我所做的就是将滑块的值范围设置为与滑块的最大宽度相同。

Hence what I did was set the value range for the slider to be the same as the max width of the slider.

然后连接滑块释放信号,检查该值是否小于最大值的一半,如果是,则将滑块值设置为0,否则将滑块值设置为max。

Then connect the slider released signal and check if the value is less than half the maximum and if so set slider value to 0 else slider value to max.

这将为您提供良好的拖动效果,并在释放鼠标时将其剪辑到极限。

This will give you a good drag effect and clip to extremes when you release the mouse.

如果您希望滑块在另一侧单击时仅在没有任何拖动的情况下进行切换,请连接滑块值更改信号并检查新值是否接近极限值或如果滑块未处于按下状态,请将其设置为滑块值 。如果滑块向下,请不要更改滑块值,因为这样可能会中断上一个拖动动作。

If you want the slider to just toggle when clicked on the other side without any drag connect the slider value changed signal and check the new value to be closer to either extreme and set it as the slider value if the slider is not in its down state. Do not change slider value if slider is down since you might then break the previous drag motion.

这篇关于Qt中的拨动开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆