qml z and transparency - KerwinKoo/KerwinKoo.github.io GitHub Wiki

Qt5.4 QML Z 序 与 透明度

z序

Item 除了 x 、 y 属性,其实还有一个 z 属性,用来指定图元在场景中的 Z 序。 z 属性的类型是 real ,数值越小,图元就越垫底(远离我们),数值越大,

透明度

Item 的属性 opacity 可以指定一个图元的透明度,取值在 0.0 到 1.0 之间。举例:

import QtQuick 2.0

Rectangle {
    width: 300;
    height: 200;

    Rectangle {		//子类1
        x: 20;
        y: 20;
        width: 150;
        height: 100;
        color: "#606080";
        z: 0.5;		//z值,在底部
    }

    Rectangle {
        width: 100;
        height: 100;
        anchors.centerIn: parent;
        color: "#a0c080";
        z: 1;		//z值大于0.5,在顶部
        opacity: 0.6;	//透明度,处于0-1之间
    }
}

技巧

除了视觉效果,有时我们也需要安排图元在场景中的 Z 序。比如一个图片浏览器,可能在加载图片时要显示一个 loading 图标,这个图标要显示在图片之上,此时就可以设置 loading 图元的 Z 序大于图片元素的 Z 序。

⚠️ **GitHub.com Fallback** ⚠️