QT QML 界面设计教程2——图标(图片)按钮样式

MyIconButton.xml

javascript 复制代码
import QtQuick 2.4
Rectangle {
    id: rec

    property alias img_src: icon.source
    property alias btn_txt: button.text

    property color clr_enter: "#dcdcdc"
    property color clr_exit: "#ffffff"
    property color clr_click: "#aba9b2"
    property color clr_release: "#ffffff"

    //自定义点击信号
    signal clickedLeft()
    signal clickedRight()
    signal release()

    width: 130
    height: 130
    radius: 10

    Image {
        id: icon
        width: 100
        height:100
        source: "qrc:/mycamera.png" //图片资源
        fillMode: Image.PreserveAspectFit
        clip: true
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 0
    }

    Text {
        id: button
        text: qsTr("button")

        anchors.top: icon.bottom
        anchors.topMargin: 5
        anchors.horizontalCenter: icon.horizontalCenter
        anchors.bottom: icon.bottom
        anchors.bottomMargin: 5

        font.bold: true
        font.pointSize: 14
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true

        //接受左键和右键输入
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
            //左键点击
            if (mouse.button === Qt.LeftButton)
            {
                parent.clickedLeft()
//                console.log(button.text + " Left button click")
            }
            else if(mouse.button === Qt.RightButton)
            {
                parent.clickedRight()
//                console.log(button.text + " Right button click")
            }
        }

        //按下
        onPressed: {
            color = clr_click
        }

        //释放
        onReleased: {
            color = clr_enter
            parent.release()
            console.log("Release")
        }

        //指针进入
        onEntered: {
            color = clr_enter
//            console.log(button.text + " mouse entered")
        }

        //指针退出
        onExited: {
            color = clr_exit
//            console.log(button.text + " mouse exited")
        }
    }
}

调用:

javascript 复制代码
        MyIconButton {
             x: mainWindow.width-40
             y: -8
             width: 20
             height: 20
             radius: 5
             id: btClose
             Image {
                 id: name
                 width: 20
                 height:20
                 source:  "qrc:/image/close.png"
             }
             btn_txt: ""
             onClickedLeft: close()
             //onClickedRight: close()
        }
相关推荐
a1117765 分钟前
微光小屋-前端养成小游戏 开源项目
前端
kyriewen18 分钟前
我让AI给前端项目做了一次完整的Code Review——它和人类的差距,比我想的大得多
前端·javascript·ai编程
张鑫旭19 分钟前
快速过下我2026年上半年的前端学了些啥
前端
liuxiaowei325 分钟前
【绝版教程】新版MySQL DBA高级实战进阶班 MySQL8.0 姜承尧-腾讯数据库总监
数据库·mysql·dba
破碎的南瓜1 小时前
sqli--sql注入过关笔记(1,2,3,4,5,9)
数据库·笔记·sql
এ慕ོ冬℘゜1 小时前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript
hoLzwEge1 小时前
拯救低效调试!code-inspector-plugin 让代码定位如虎添翼
前端·前端框架·node.js
J船长1 小时前
烂笔头扫盲:LLM Eval 入门:从「感觉挺好」到「数据说话」的工程化落地
前端
skiyee1 小时前
被多个 UView 系 UI 库 “抄” 的 @uni-ku/root 究竟怎么实现?
前端