【QML】QML自定义按钮

一、引言

在qml开发中,本文总结自己在开发中比较喜欢的实现自定义按钮的两种方式:
(1)Button+states
(2)Image + MouseArea

【效果如下】

请添加图片描述

二、自定义按钮
2-1、以贴图方式实现自定义按钮

(直接上码啦)

//【Button + states 实现】

import QtQuick 2.10
import QtQuick.Templates 2.1 as T

T.Button {
    id: control
    implicitWidth: Math.max(background ? background.implicitWidth : 0,
                            iconOn.implicitWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(background ? background.implicitHeight : 0,
                             iconOn.implicitHeight + topPadding + bottomPadding)
    leftPadding: 4
    rightPadding: 4

    text: control.state
    property alias iconOffSource: iconOff.source
    property alias iconOnSource: iconOn.source
    autoExclusive: false
    checkable: true


    Image {
        id: iconOn
        x: 0
        y: 0
        source: "qrc:/asserts/images/setting_normal.png"
    }

    Image {
        id: iconOff
        x: 0
        y: 0
        source: "qrc:/asserts/images/setting_checked.png"
    }

    //创建按钮的“已选择”和“正常”两种状态
    states: [
        //已选择状态
        State {
            name: "checked"
            when: control.checked

            PropertyChanges {
                target: iconOff
                visible: false
            }
        },

        //正常状态
        State {
            name: "normal"
            when: !control.pressed && !control.checked &&!control.hovered

            PropertyChanges {
                target: iconOn
                visible: false
            }
        }
    ]
}

//【Image + MouseArea实现】

import QtQuick 2.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3


Image {
    id: root
    source: "qrc:/asserts/images/settingsIcon.png"
    signal clicked

    property int duration: 500
    property alias text: label.text

    MouseArea {
        anchors.fill: parent
        anchors.bottomMargin: -29
        onClicked: root.clicked()
        onPressed: {
            glow.visible = true
            animation1.start()
            animation2.start()
        }
    }

    Rectangle {
        id: glow
        x: 34
        y: 39
        visible: false
        width: 250
        height: 250
        color: "#00000000"
        radius: 125
        scale: 1.05
        border.color: "yellow"
    }

    Label {
        id: label
        x: 292
        y: 328
        text: qsTr("Label")
        anchors.horizontalCenter: parent.horizontalCenter
        color: "white"
        font.family: Constants.fontFamily
        font.pixelSize: 28
        anchors.horizontalCenterOffset: 0
    }

    PropertyAnimation {
        target: glow
        id: animation1
        duration: root.duration
        loops: 1
        from: 1.05
        to: 1.1
        property: "scale"
    }

    ParallelAnimation {
        id: animation2
        SequentialAnimation {
            PropertyAnimation {
                target: glow
                duration: root.duration
                loops: 1
                from: 0.2
                to: 1.0
                property: "opacity"
            }
            PropertyAnimation {
                target: glow
                duration: root.duration
                loops: 1
                from: 1.0
                to: 0.0
                property: "opacity"
            }

            PropertyAction {
                target: glow
                property: "visible"
                value: false
            }
        }

        SequentialAnimation {
            PropertyAction {
                target: glow
                property: "border.width"
                value: 50
            }

            PauseAnimation {
                duration: 20
            }

            PropertyAnimation {
                target: glow
                duration: root.duration
                loops: 2
                from: 50
                to: 10
                property: "border.width"
            }
        }
    }
}
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值