Qml之自定义Button

2 篇文章 0 订阅
2 篇文章 0 订阅


前言

提示: 自定义Button控件如何分为带图片的Iconbutton和原生控件重写的Button。最终效果如下:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、图标Button

import QtQuick 2.15
import QtQuick.Controls 2.15

Item {
    id: item1
    property alias imageProperty: image_Rect
    property alias imageBasic: id_image

    property real scale: 1.0  // 图片的缩放比例

    property int imageWidth: 32 // 图片宽度
    property int imageHeight: 32 // 图片高度
    property string imageSource: ""  // 图片资源
    property int radiusSize: 10 // 圆角值
    property color bordercolor: "#99CCCC"//图标border
    property int btnSize: 16// 图标下方字体尺寸
    property string btnfontfamily: "黑体"//文字字体
    property string buttonName: "Test"//button 名称

    signal clicked  // 点击信号
    signal pressed  // 按下信号
    signal released // 松开信号

    Rectangle {

        id: image_Rect
        width: item1.imageWidth+2
        height: item1.imageHeight+2

        radius: item1.radiusSize
        border.width: 1
        border.color: bordercolor
        /*
            Image.Stretch: 将图像拉伸以填充可用空间,不保持纵横比
            Image.PreserveAspectFit: 保持图像的纵横比,将图像缩放到适合可用空间内的最大尺寸
            Image.PreserveAspectCrop: 保持图像的纵横比,将图像缩放到填充可用空间的最小尺寸,并裁剪超出可用区域的部分
            Image.Tile: 将图像平铺以填充可用空间,不进行缩放
            Image.TileVertically: 将图像垂直平铺以填充可用空间,不进行缩放
            Image.TileHorizontally: 将图像水平平铺以填充可用空间,不进行缩放
        */
        //图标
        Image {
            id: id_image
            source: item1.imageSource
            fillMode: Image.PreserveAspectFit
            anchors.centerIn: parent
            smooth: true  // 使用平滑插值显示图像
            asynchronous: true  // 异步加载图像,不阻塞QML引擎执行其他任务
            transform: Scale {
                xScale: image_Rect.scale
                yScale: image_Rect.scale
            }
            state: "released"
            states: [
                State {
                    name: "released"
                    PropertyChanges { target: id_image; scale: 1 }
                    PropertyChanges { target: id_image; rotation: 0 }
                },
                State {
                    name: "clicked"
                    PropertyChanges { target: id_image; scale: 0.6 }
                    PropertyChanges { target: id_image; rotation: 180 }
                },
                State {
                    name: "hovered"
                    PropertyChanges { target: id_image; scale: 1.2 }
                }
            ]
            transitions: [
                Transition
                {
                    from: "released"; to: "clicked"
                    NumberAnimation { target: id_image; properties: "scale"; duration: 100 }
                    RotationAnimation { target: id_image; properties: "rotation"; duration: 100 }
                },
                Transition
                {
                    from:"clicked" ; to: "released"
                    NumberAnimation { target: id_image; properties: "scale"; duration: 100 }
                    RotationAnimation { target: id_image; properties: "rotation"; duration: 100 }
                },
                Transition
                {
                    from:"released" ; to: "hovered"
                    NumberAnimation { target: id_image; properties: "scale"; duration: 100 }
                }
            ]
        }

        //文字
        Text {
            id: btnName
            text: buttonName
            font.bold: true
            font.pixelSize:btnSize
            font.family:btnfontfamily
            anchors.topMargin: 2
            anchors.top: id_image.bottom
            anchors.horizontalCenter: id_image.horizontalCenter

            states: [
                State {
                    name: "released"
                    PropertyChanges { target: btnName; scale: 1   }
                },
                State {name: "clicked"
                    PropertyChanges { target: btnName; scale: 0.85 }
                }
            ]
            transitions: [
                Transition
                {
                    from: "released"; to: "clicked"
                    NumberAnimation { target: btnName; properties: "scale"; duration: 100 }
                },
                Transition
                {
                    from:"clicked" ; to: "released"
                    NumberAnimation { target: btnName; properties: "scale"; duration: 100 }
                }
            ]

        }

        //鼠标事件以及信号发送
        MouseArea {
            id: mouseArea
            hoverEnabled: true
            anchors.fill: parent
            // 发射信号,外部实现具体需求
            onClicked:
            {
                item1.clicked()
                //console.log("clicked")
            }
            //鼠标按下
            onPressed: {
                item1.pressed()
                id_image.state="clicked"
                btnName.state="clicked"
                //console.log("Pressed")
            }
            //鼠标松开
            onReleased: {
                item1.released()
                id_image.state="released"
                btnName.state="released"
                //console.log("released")
            }
            //鼠标进入
            onEntered: {
                id_image.scale=1.2
                //id_image.state="hovered"
                //console.log("mouse in")
            }
            //鼠标退出
            onExited: {
                id_image.scale=1
                //console.log("mouse out")
            }
        }

    }
}

二、字体Button

1.重写Background和ContentItem

将Button的Background和contentItem重写就可以完全改造Button。Background负责button外观,contentItem负责显示内容。

import QtQuick 2.15
import QtQuick.Controls 2.15

Button{
    id:control



    background:  Rectangle{
        id:back
        anchors.fill: parent
        anchors.centerIn: parent
        radius: 10
        scale:control.hovered? 1.1 :1
        color: control.pressed ?"#006633" : "#009966"
        Behavior on scale { NumberAnimation{duration:200 }}
        Behavior on color { ColorAnimation{duration:200 }}
    }

    contentItem:Text {
        id: contentText
        text: qsTr("Button")
        anchors.centerIn: back
        font.bold:true
        font.family: "黑体"
        font.pixelSize: 20
        scale:control.hovered? 1.3 :1
        color:control.pressed ?"#996699" : "#FFFFFF"
        Behavior on scale { NumberAnimation{duration:200 }}
        Behavior on color { ColorAnimation{duration:200 }}
    }




}


2.采用ButtonStyle

另外一种方式需要改写buttonStyle,这种方法需要 import QtQuick.Controls.Styles ,不像上面那种方便快捷

import QtQuick 2.9
import QtQuick.Controls 1.5
import QtQuick.Controls.Styles 1.4

Button {
    id:control

    style: ButtonStyle {
        background: Rectangle {
            implicitWidth: 64
            implicitHeight: 32
            scale:control.hovered? 1.2 :1
            color: control.pressed ?"#006633" : "#009966"
            border.width:  0
            radius: 10
            Behavior on scale { NumberAnimation{duration:200 }}
            Behavior on color { ColorAnimation{duration:200 }}
            Text {
                id: name
                font.bold: true
                font.family: "黑体"
                font.pointSize: 16
                scale:control.hovered? 1.1 :1
                color:/*control.hovered?"#003333":*/"#FFFFFF"
                anchors.centerIn: parent
                text: qsTr("text")
                Behavior on scale { NumberAnimation{duration:200 }}
                Behavior on color { ColorAnimation{duration:200 }}
            }
        }
    }

}


  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YZW0123

谢谢大家的支持,请关注我哦!!

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

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

打赏作者

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

抵扣说明:

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

余额充值