QML自定义按钮,按钮中包含图片和文字

7 篇文章 0 订阅

按钮效果如下

直接上代码

import QtQuick 2.0

Rectangle {
    id:iconButton
    width:parent.width
    height:parent.height
    color: "#6CADFF"
    radius:8
    //用于外部调用时,输入图片的地址
    property string imageSource : image.source
    //用于外部调用时,输入按钮名称
    property string descText : btnText.text
    //添加信号
    signal buttonClicked()

    Image{
        id:image
        x:13
        y:13
        width:15
        height:15
        fillMode: Image.PreserveAspectFit
        horizontalAlignment: Image.AlignHCenter
        verticalAlignment: Image.AlignVCenter
        source: imageSource
    }

    Text{
        id:btnText
        text: descText
        x:image.x + image.width + image.x
        height: parent.height
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
        color: "white"
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true // 启用悬停事件

        onEntered: {
            iconButton.color = "#7DB8FF"; // 鼠标进入时改变颜色
        }

        onExited: {
            iconButton.color = "#6CADFF"; // 鼠标离开时恢复颜色
        }

        onPressed: {
            iconButton.color = "#6CADFF";
        }

        onReleased: {
            iconButton.color = "#7DB8FF";
        }

        onClicked:{
            iconButton.buttonClicked();
        }
    }
}

外部应用IconButton例子

    IconButton{
        x:bottomItem.width - 3 * 118 - 2 * 10
        y:bottomItem.height - 70
        width: 118
        height: 47
        descText: "保存图片"
        imageSource: "resources/SaveImage.png"
        fontSize:15
        onButtonClicked:{
            console.log("mainWindow.widht = " + mainWindow.width)
        }
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值