QML QT6自定义开关控件 Switch

自定义开关控件,效果一般,相互学习,谢谢。源码和示例如下:

Custom Switch Examples

import QtQuick 2.15
import QtQuick.Controls 2.15
 
ApplicationWindow {
    visible: true
    width: 300
    height: 600
    title: "Custom Switch Examples"
 
    property bool switchChecked: false
 
    Row {
        spacing: 100
        anchors.centerIn: parent
 
        Column {
            spacing: 50
 
            Text {
                text: "卡通风格的开关"
                font.family: "Arial"
                font.bold: true
                font.pixelSize: 24
                color: "#333333"
                anchors.horizontalCenter: parent.horizontalCenter
            }
 
 
            Rectangle {
                width: 80
                height: 40
                radius: 20
                color: switchChecked ? "#4CAF50" : "#CCCCCC"
                border.color: "black"
                border.width: 1
 
                // Handle
                Rectangle {
                    width: 35
                    height: 35
                    radius: 17.5
                    color: switchChecked ? "white" : "gray"
                    border.color: "black"
                    border.width: 1
                    x: switchChecked ? parent.width - width - 5 : 5
                    y: (parent.height - height) / 2
                    // Add text inside the handle
                    Text {
                        anchors.centerIn: parent
                        text: switchChecked ? "开" : "关"
                        color: switchChecked ? "black" : "white"
                        font.bold: true
                        font.pixelSize: 14
                    }
                    Behavior on x {
                        NumberAnimation { duration: 200 }
                    }
                }
 
                MouseArea {
                    anchors.fill: parent
                    onClicked: switchChecked = !switchChecked
                }
            }
 
 
            Rectangle {
                width: 80
                height: 40
                radius: 20
                color: switchChecked ? "#3F51B5" : "#B0BEC5"
                border.color: "black"
                border.width: 1
 
                // Handle with shadow
                Rectangle {
                    width: 35
                    height: 35
                    radius: 17.5
                    color: switchChecked ? "white" : "#FFC107"
                    border.color: "black"
                    border.width: 1
                    x: switchChecked ? parent.width - width - 5 : 5
                    y: (parent.height - height) / 2
                    // Add text inside the handle
                    Text {
                        anchors.centerIn: parent
                        text: switchChecked ? "开" : "关"
                        color: switchChecked ? "black" : "black"
                        font.bold: true
                        font.pixelSize: 14
                    }
                    Behavior on x {
                        NumberAnimation { duration: 200 }
                    }
                }
 
                MouseArea {
                    anchors.fill: parent
                    onClicked: switchChecked = !switchChecked
                }
            }
 
 
            Rectangle {
                width: 80
                height: 40
                radius: 20
                color: switchChecked ? "#BBDEFB" : "#E0E0E0"
                border.color: "black"
                border.width: 1
 
                // Handle
                Rectangle {
                    width: 35
                    height: 35
                    radius: 17.5
                    color: switchChecked ? "#2196F3" : "#FFFFFF"
                    border.color: switchChecked ? "#1976D2" : "#B0BEC5"
                    border.width: 2
                    x: switchChecked ? parent.width - width - 5 : 5
                    y: (parent.height - height) / 2
                    // Add text inside the handle
                    Text {
                        anchors.centerIn: parent
                        text: switchChecked ? "开" : "关"
                        color: switchChecked ? "white" : "black"
                        font.bold: true
                        font.pixelSize: 14
                    }
                    Behavior on x {
                        NumberAnimation { duration: 200 }
                    }
                }
 
                MouseArea {
                    anchors.fill: parent
                    onClicked: switchChecked = !switchChecked
                }
            }
 
 
            Rectangle {
                width: 80
                height: 40
                radius: 20
                color: switchChecked ? "#E91E63" : "#B0BEC5"
                border.color: "black"
                border.width: 1
 
                // Handle with gradient
                Rectangle {
                    width: 35
                    height: 35
                    radius: 17.5
                    color: "white"
                    border.color: "black"
                    border.width: 1
                    x: switchChecked ? parent.width - width - 5 : 5
                    y: (parent.height - height) / 2
                    gradient: Gradient {
                        GradientStop { position: 0.0; color: switchChecked ? "#FF9800" : "#FF5722" }
                        GradientStop { position: 1.0; color: switchChecked ? "#FF5722" : "#FF9800" }
                    }
                    // Add text inside the handle
                    Text {
                        anchors.centerIn: parent
                        text: switchChecked ? "开" : "关"
                        color: "black"
                        font.bold: true
                        font.pixelSize: 14
                    }
                    Behavior on x {
                        NumberAnimation { duration: 200 }
                    }
                }
 
                MouseArea {
                    anchors.fill: parent
                    onClicked: switchChecked = !switchChecked
                }
            }
        }
 
        Column {
            spacing: 50
 
            Text {
                text: "自定义的开关"
                font.family: "Arial"
                font.bold: true
                font.pixelSize: 24
                color: "#333333"
                anchors.horizontalCenter: parent.horizontalCenter
            }
 
            ListModel {
                id: switchModel
                ListElement { label: "Wi-Fi"; style: "circle" }
                ListElement { label: "Wi-Fi"; style: "text" }
                ListElement { label: "Wi-Fi"; style: "textWithLabel" }
                ListElement { label: "Bluetooth"; style: "modern" }
            }
 
            Repeater {
                model: switchModel
 
                Rectangle {
                    width: 100
                    height: 40
                    radius: height / 2
                    color: {
                        switch (model.style) {
                        case "circle":
                            return switchChecked ? "lightgreen" : "lightcoral";
                        case "text":
                            return switchChecked ? "lightyellow" : "lightgray";
                        case "textWithLabel":
                            return switchChecked ? "#D3F8D3" : "#F8D3D3";
                        case "modern":
                            return switchChecked ? "#4CAF50" : "#B0BEC5";
                        default:
                            return "gray";
                        }
                    }
                    border.color: "gray"
                    border.width: 1
 
                    // Handle
                    Rectangle {
                        width: 35
                        height: 35
                        radius: 17.5
                        color: "white"
                        border.color: "black"
                        border.width: 1
                        x: switchChecked ? parent.width - width - 5 : 5
                        y: (parent.height - height) / 2
 
                        // Add text inside the handle
                        Text {
                            anchors.centerIn: parent
                            text: switchChecked ? "开" : "关"
                            color: "black"
                            font.bold: true
                            font.pixelSize: 14
                        }
                        Behavior on x {
                            NumberAnimation { duration: 200 }
                        }
                    }
 
                    MouseArea {
                        anchors.fill: parent
                        onClicked: switchChecked = !switchChecked
                    }
                }
            }
        }
 
        Column {
            spacing: 50
 
            Text {
                text: "系统默认开关"
                font.family: "Arial"
                font.bold: true
                font.pixelSize: 24
                color: "#333333"
                anchors.horizontalCenter: parent.horizontalCenter
            }
 
            Switch {
                text: qsTr("Wi-Fi")
                checked: switchChecked
            }
            Switch {
                text: qsTr("Bluetooth")
                checked: switchChecked
            }
            Switch {
                text: qsTr("Wi-Fi")
                checked: switchChecked
            }
            Switch {
                text: qsTr("Bluetooth")
                checked: switchChecked
            }
        }
    }
}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值