Toggle Switch in Qt

后端 未结 10 2200
臣服心动
臣服心动 2020-12-12 17:31

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

10条回答
  •  臣服心动
    2020-12-12 18:07

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

    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.

    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.

提交回复
热议问题