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"
}
}
}