8. QML封装控件 --- 椭圆按钮

该文章展示了如何使用QtQuick创建一个自封装的椭圆形状按钮,包括设置颜色属性、点击事件、文字内容和尺寸。代码中利用ShapePath绘制了左右两个半圆形成椭圆按钮,并添加了点击时的缩放动画效果。同时,按钮具备可配置的文字颜色和大小。
摘要由CSDN通过智能技术生成

1. 效果展示:

自封装椭圆按钮

2. 整体代码:

import QtQuick 2.12
import QtQuick.Shapes 1.15

Rectangle {
    id:root
    anchors.centerIn: parent
    implicitWidth: 50
    implicitHeight: 40
    color: btnColor

    //颜色属性
    property color btnColor: "lightgray"
    //点击事件属性
    property var btnFunc:function(){}
    //按钮文字属性
    property string btnText:"Button"
    //文字颜色属性
    property color textColor:"black"
    //文字尺寸属性
    property int textSize:16

    //绘制左半圆
    Shape {
        anchors.left: parent.left
        width: root.height
        height: root.height
        ShapePath {
            strokeWidth: 1
            strokeColor: root.btnColor
            fillColor: root.btnColor
            //指定绘制起点
            startX: 0
            startY: 0
            PathArc {
                //指定绘制终点
                x: 0
                y: root.height
                radiusX: root.height * 0.5
                radiusY: root.height * 0.5
                useLargeArc: true
                //指定绘制方向为逆时针
                direction: PathArc.Counterclockwise
            }
        }
    }
    //绘制右半圆
    Shape {
        anchors.right: parent.right
        width: root.height
        height: root.height
        ShapePath {
            strokeWidth: 1
            strokeColor: root.btnColor
            fillColor: root.btnColor
            //指定绘制起点
            startX: root.height
            startY: 0
            PathArc {
                //指定绘制终点
                x: root.height
                y: root.height
                radiusX: root.height * 0.5
                radiusY: root.height * 0.5
                useLargeArc: true
                //指定绘制方向为顺时针
                direction: PathArc.Clockwise
            }
        }
    }
    //点击触发动画
    SequentialAnimation{
        id:anim
        running: false
        PropertyAnimation {
            id: scaleSAnim
            target: root
            property: "scale"
            to: 0.9
            easing.type: Easing.OutQuad
            duration: 50
        }
        PropertyAnimation {
            id: scaleLAnim
            target: root
            property: "scale"
            to: 1.0
            easing.type: Easing.OutQuad
            duration: 50
        }

    }
    //鼠标控制区域
    MouseArea {
        anchors.fill: parent
        onClicked: {
            anim.running = true
            root.btnFunc()
        }
    }
    //按钮文字
    Text {
        text: root.btnText
        color: root.textColor
        font.bold: true
        font.pixelSize: root.textSize
        anchors.centerIn: parent
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山间点烟雨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值