1. QML封装控件---开关按钮

该文章展示了如何用QtQuick编写一个自定义的开关按钮组件,包括开关状态的切换效果,颜色变化以及文本显示。代码中定义了开关的打开和关闭状态,并通过动画实现平滑过渡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果展示:

自制开关按钮

整体代码:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.0

Item{
    id:switchBtnRoot
    implicitWidth: 60
    implicitHeight: 20

    //用于判断开关状态,外界可获取该状态值进行响应
    property bool isOpen: false

    Rectangle{
        id:bg
        anchors.fill: parent
        radius: switchBtnRoot.width/2
        state: !isOpen ? "close" : "open"

        property alias opentxtVis: openText.visible
        property alias closetxtVis: closeText.visible

        Text{
            id:openText
            anchors.verticalCenter: bg.verticalCenter
            anchors.left: bg.left
            anchors.leftMargin: 8
            text: "ON"
            color: "#F6F6F6"
            font.pixelSize: bg.height * 0.6
            visible: opentxtVis
        }
        Text{
            id:closeText
            anchors.verticalCenter: bg.verticalCenter
            anchors.right: bg.right
            anchors.rightMargin: 8
            text: "OFF"
            color: "#F6F6F6"
            font.pixelSize: bg.height * 0.6
            visible: closetxtVis
        }

        states: [
            State {
                name: "close"
                PropertyChanges {
                    target: bg
                    color:"#949599"
                    opentxtVis:false
                    closetxtVis:true
                }
            },
            State {
                name:"open"
                PropertyChanges {
                    target: bg
                    color:"#46C636"
                    opentxtVis:true
                    closetxtVis:false
                }
            }
        ]
        Rectangle{
            id:slider
            x:bg.x
            anchors.verticalCenter: bg.verticalCenter
            width: bg.height
            height: width
            radius: width/2
            color: "#F6F6F6"
            border.color:"#7D8184"
            border.width:1
            NumberAnimation on x{
                id:openAnim
                from: 0
                to:bg.width-slider.width
                duration: 250
                running: isOpen
                loops: 1
            }
            NumberAnimation on x{
                id:closeAnim
                from: bg.width-slider.width
                to:0
                duration: 250
                running: false
                loops: 1
            }
        }
        MouseArea{
            anchors.fill: parent
            onClicked: {
                switchBtnRoot.isOpen = !switchBtnRoot.isOpen
                if(bg.state === "close"){
                    closeAnim.start()
                }
            }
        }
    }
}

持续更新中,请大家多多关注…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山间点烟雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值