QML实现带.阴影按钮

QML自带的按钮效果不是很好,所以自定义了一个按钮。QML也支持这种高度自定义控件。

主要有阴影效果,悬浮变色和按钮动画。效果如下

主要代码如下 

MyButton.qml

import QtQuick 2.6
import QtGraphicalEffects 1.0

/****************************************************************************
**自定义按钮功能实现
**主要实现按钮的按压效果,带有阴影效果
**
****************************************************************************/

Item {
    id : mybutton
    width: 100
    height: 50

    signal clicked();

    Rectangle {
        id : bgrect;
        y : 20
        x : 1
        color: "#5CB85C";
        width: mybutton.width-2;
        height: mybutton.height-25
        radius: height/2
    }

    DropShadow {
        id : shadow
        anchors.fill: bgrect
        horizontalOffset: 2
        verticalOffset: 12
        radius: 8.0
        samples: 17
        color: "#999999"
        source: bgrect
    }


    Rectangle{
        id : toprect
        color: "#5CB85C"
        width: mybutton.width;
        height: mybutton.height-2
        radius: height/3

        MouseArea {
            id: mouseArea
            anchors.fill: parent
            hoverEnabled : true
            onClicked: {
               animation.start();
               mybutton.clicked();
            }
            onEntered: {
                toprect.color = "#3e8e41";
                bgrect.color = "#3e8e41";
            }
            onExited: {
                toprect.color = "#5CB85C";
                bgrect.color = "#5CB85C";
            }

        }

    }

    Text {
        id: mytext
        anchors.centerIn: toprect
        text: qsTr("text")
        color: "#ffffff"
        font.pixelSize : toprect.height/2
    }


    SequentialAnimation {

              id : animation
              NumberAnimation { target: toprect; property: "y"; to: toprect.x+2; duration: 100 }
              NumberAnimation { target: toprect; property: "y"; to: toprect.x-2; duration: 100 }
          }

}

使用如下

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("自定义Button")

    MyButton{
        x : 100; y : 100
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值