qml 学习自定义button的坑

平台:qtcreator 5.12.6  win10

问题:无法显示图标,设置其他icon的属性(无效)

总结:button的一些自定义用法(包括渐变色,三态,图标,背景图)

来源:qt官网的examples,书籍 qtquick核心编程

 

问题解决方案:

译文:

只要把 color 设置为透明,他就可以不用跟随文字的颜色。显示出来

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Styles 1.4
Rectangle {
    visible: true
    width: 300
    height: 700
    Button {  //渐变色
        id:control
        text: "A button"
        background: Rectangle {
            implicitWidth: 200
            implicitHeight: 50
            border.width: control.activeFocus ? 2 : 1
            border.color: "red"
            radius: 4
            gradient: Gradient {
                GradientStop { position: 0 ; color: control.pressed ? "#0000ff" : "#f0f8ff" }
                GradientStop { position: 1 ; color: control.pressed ? "#00ffff" : "#8a2be2" }
            }
        }
    }

    Button{//三态
        id:btn
        text:"三态"
        width: control.width
        height: control.height
        anchors.top: control.bottom
        anchors.topMargin: 10

        background:Rectangle {
            border.width: btn.pressed?2:1;
            border.color:{
                if(btn.pressed)
                    "#006400"
                else if(btn.hovered)
                    "#0000ff"
                else
                    "#dc143c";
            }
        }
    }

    Button{  //三态 内部
        id:btnIn
        text:"内部"
        anchors{top: btn.bottom;topMargin: 10}
        implicitWidth: 200;implicitHeight: 50

        background:Rectangle {
            color:{if(btnIn.pressed)
                    "#006400"
                else if(btnIn.hovered)
                    "#0000ff"
                else
                    "#dc143c"

            }
        }
    }

    Button{//图标
        id:iconx
        anchors{top: btnIn.bottom;topMargin: 10}
        //        implicitWidth: 200;implicitHeight: 50

        text:"谷歌浏览器"
        icon.source: "qrc:/icon.png"
        icon.color: "transparent"
        display: AbstractButton.TextBesideIcon
        leftPadding: 0  //控制空格
    }

    Button{
        anchors{top: iconx.bottom;topMargin: 10}
        Text {
            text: "背景按钮"
            color: "red"
            font.pointSize: 10
        }
        //第一种
//        Image {
//            anchors.fill: parent
//            source: "qrc:/icon.png"
//        }
        //第二种
        icon.source: "qrc:/icon.png"
        icon.color:"transparent"
        
    }


}

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值