qml设置透明度

qml设置透明度有两个方式,一种是Qt.rgba()方法是:将十六进制转成rgb,每个数值都除以255,另一种是opacity属性值设置,但是这两种是有区别的,Qt.rgba()设置只能当前的Rectangle有效,而opacity的设置会影响到子部件,下面看源码示例

import QtQuick 2.13
import QtQuick.Window 2.13

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle {
        id: rect1;
        anchors.top: parent.top
        anchors.topMargin: 20
        anchors.left: parent.left
        anchors.leftMargin: 20
        color: "#4d4ca7"
        width: 100
        height: 60
        Rectangle {
            anchors{top: parent.top; topMargin: 20; left: parent.left; leftMargin: 30}
            width: 40; height: 30
            color: "red"
        }
    }

    Rectangle {
        id: rect2;
        anchors.top: parent.top
        anchors.topMargin: 50
        anchors.left: parent.left
        anchors.leftMargin: 150
        color: Qt.rgba(77/255,76/255,167/255, 0.5)//设置透明度   每个数值都除以255
        width: 100
        height: 60
        Rectangle {
            anchors{top: parent.top; topMargin: 20; left: parent.left; leftMargin: 30}
            width: 40; height: 30
            color: "red"
        }
    }

    Rectangle {
        id: rect3;
        anchors.top: parent.top
        anchors.topMargin: 80
        anchors.left: parent.left
        anchors.leftMargin: 280
        color: "#4d4ca7"
        opacity: 0.5
        width: 100
        height: 60
        Rectangle {
            anchors{top: parent.top; topMargin: 20; left: parent.left; leftMargin: 30}
            width: 40; height: 30
            color: "red"
        }
    }
}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值