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()
        }
相关推荐
数据与后端架构提升之路4 小时前
系统架构设计师常见高频考点总结之数据库
数据库·系统架构
xixingzhe25 小时前
MySQL CDC实现方案
数据库·mysql
这是个栗子5 小时前
前端开发中的常用工具函数(二)(持续更新中...)
开发语言·前端·javascript
苦藤新鸡5 小时前
38.交换二叉树中所有的左右节点
开发语言·前端·javascript
tqs_123455 小时前
tcc中的空回滚和悬挂问题
java·数据库
2501_944521595 小时前
Flutter for OpenHarmony 微动漫App实战:主题配置实现
android·开发语言·前端·javascript·flutter·ecmascript
哪里不会点哪里.5 小时前
Spring 事务机制详解:原理、传播行为与失效场景
java·数据库·spring
IT大白6 小时前
8、MySQL相关问题补充
数据库·sql
爪哇天下6 小时前
Mysql实现经纬度距离的排序(粗略的城市排序)
数据库·mysql
独自破碎E6 小时前
MySQL中有哪些日志类型?
数据库·mysql