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()
        }
相关推荐
qq_4557608510 分钟前
redis - 集群
数据库·redis·缓存
Li_76953212 分钟前
Redis 进阶(五)—— 哨兵
数据库·redis·缓存
困知勉行198514 分钟前
Redis大key处理
数据库·redis·缓存
扑火的小飞蛾18 分钟前
oracle SR模板参考
数据库·oracle
搬砖的kk23 分钟前
openJiuwen 快速入门:使用华为云大模型搭建 AI 智能体
数据库·人工智能·华为云
天蓝色的鱼鱼23 分钟前
别再瞎转Base64了!一文打通前端二进制任督二脉
前端
哆啦code梦26 分钟前
一文认识Redis
数据库·redis·缓存
哟哟耶耶26 分钟前
Plugin-安装Vue.js devtools6.6.3扩展(组件层级可视化)
前端·javascript·vue.js
路漫聊架构30 分钟前
Redis扫描大key利器Scan命令探秘
java·数据库·redis
梦65038 分钟前
【前端实战】图片元素精准定位:无论缩放,元素始终钉在指定位置
前端·html·css3