Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
import QtQuick 2.2
import QtQuick.Controls 1.2

Rectangle 
{
    id
: doubleSlider
    width: 360
    height: 72
    color: "#c3c3c3"

    property int minValue: 0
    property int maxValue: 100
    property int lowValue: 0
    property int highValue: 100
    property int thumbWidth: 12
    readonly property int range: width - thumbWidth
    property var thumbColor: "#c030bb"
    property int lastX: 0

    signal value(int low,int high)

    Rectangle {
        id: startSlider
        width: doubleSlider.thumbWidth
        height: parent.height

        x: 0
        anchors.verticalCenter: parent.verticalCenter

        color: doubleSlider.thumbColor
    
}

    Rectangle 
{
        id
: endSlider
        width: doubleSlider.thumbWidth
        height: parent.height

        x: parent.width - endSlider.width
        anchors.verticalCenter: parent.verticalCenter

        color: doubleSlider.thumbColor
    
}

    MouseArea 
{
        id
: sliderMouseArea
        anchors.fill: parent
        hoverEnabled: true

        property point clickPos
        property var currentSlider: undefined

        onPressed: {
            updateCurrentSlider(mouse)
            clickPos = Qt.point(mouse.x,mouse.y)
        
}

        onPositionChanged:
{
            if(pressed && currentSlider != undefined){
                var delta = Qt.point(mouse.x-clickPos.x,mouse.y-clickPos.y)
                doubleSlider.lastX = currentSlider.x
                currentSlider.x = mouse.x
                fixSliderPosition()
            
}
        }

        onWheel: 
{
            if(currentSlider === undefined)
                return

            var delta = wheel.angleDelta.y/120
            currentSlider.x += delta
            fixSliderPosition()
        
}

        function fixSliderPosition()
{
            if(currentSlider === undefined)
                return

            if(currentSlider === startSlider &&
                    startSlider.x+startSlider.width > endSlider.x)
                currentSlider.x = endSlider.x-startSlider.width

            if(currentSlider === endSlider &&
                    endSlider.x < startSlider.x + startSlider.width)
                currentSlider.x = startSlider.x + startSlider.width


            if(currentSlider.x < 0)
                currentSlider.x = 0
            if(currentSlider.x > doubleSlider.width - currentSlider.width)
                currentSlider.x = doubleSlider.width - currentSlider.width

            if(currentSlider === startSlider){
                var v0 = 100*startSlider.x/doubleSlider.range
                if(v0 != lowValue){
                    lowValue = v0
                    doubleSlider.value(lowValue,highValue)
                
}
            }
            else if(currentSlider === endSlider)
{
                var v1 = 100*endSlider.x/doubleSlider.range
                if(v1 != highValue){
                    highValue = v1
                    doubleSlider.value(lowValue,highValue)
                
}
            }
        }

        function containPoint(input,minValue,maxValue)
{
            if(input < minValue || input > maxValue)
                return false
            return true
        
}

        function updateCurrentSlider(mouse)
{
            if(containPoint(mouse.x,startSlider.x,startSlider.x+startSlider.width)){
                currentSlider = startSlider
                doubleSlider.lastX = startSlider.x
            
}
            else if(containPoint(mouse.x,endSlider.x,endSlider.x+endSlider.width))
{
                currentSlider = endSlider
                doubleSlider.lastX = endSlider.x
            
}
        }
    }
}
posted on 2019-09-11 09:47 ccsdu2009 阅读(11) 评论(0)  编辑 收藏 引用 所属分类: QT编程

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理