15

如何Rectangle在 QtQuick 2.0 上为视觉项目绘制阴影?
我喜欢为我的主窗口画一个阴影(我有一个透明且没有装饰的窗口)

4

4 回答 4

16

作为裁剪阴影问题的解决方法,您可以将您RectangleItem, 附加边距考虑到模糊半径,并在该容器上应用阴影:

import QtQuick 2.0
import QtGraphicalEffects 1.0

Item {
    width: 320
    height: 240

    Item {
        id: container
        anchors.centerIn: parent
        width:  rect.width  + (2 * rectShadow.radius)
        height: rect.height + (2 * rectShadow.radius)
        visible: false

        Rectangle {
            id: rect
            width: 100
            height: 50
            color: "orange"
            radius: 7
            antialiasing: true
            border {
                width: 2
                color: "red"
            }
            anchors.centerIn: parent
        }
    }

    DropShadow {
        id: rectShadow
        anchors.fill: source
        cached: true
        horizontalOffset: 3
        verticalOffset: 3
        radius: 8.0
        samples: 16
        color: "#80000000"
        smooth: true
        source: container
    }
}
于 2013-03-26T14:18:51.383 回答
8

只需DropShadowQtGraphicalEffects模块中使用。

一个完整的工作示例:

import QtQuick 2.0
import QtGraphicalEffects 1.0

Rectangle {
    width: 640
    height: 480
    color: "blue"

    Rectangle {
        id: rect
        anchors.centerIn: parent
        width: 100
        height: 100
        color: "red"
    }

    DropShadow {
        anchors.fill: rect
        cached: true
        horizontalOffset: 3
        verticalOffset: 3
        radius: 8.0
        samples: 16
        color: "#80000000"
        source: rect
    }
}

请注意,您将看到许多类似这样的警告:

file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/DropShadow.qml:391:5:QML SourceProxy:检测到属性“输出”的绑定循环 file:///opt/Qt5.0.1 /5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml:66:5:QML SourceProxy:检测到属性“输出”文件的绑定循环:///opt/Qt5.0.1/5.0.1/gcc_64/qml /QtGraphicalEffects/private/GaussianDirectionalBlur.qml:61:5:QML SourceProxy:检测到属性“输出”文件的绑定循环:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml :66:5:QML SourceProxy:检测到属性“输出”文件的绑定循环:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml:61:5:QML SourceProxy:为属性“输出”文件检测到绑定循环:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianGlow.qml:53:5:QML SourceProxy:检测到属性“输出”的绑定循环

这些警告是QTBUG-28521,已在 Qt 5.0.2 中修复(在撰写本文时尚未发布)。幸运的是,除了烦人的控制台输出之外,没有任何实际问题。

于 2013-03-19T01:39:24.490 回答
5

有趣的问题......我一直在寻找更好的方法来做到这一点。这是我暂时为 QML 矩形实现投影效果的快速而肮脏的方法。

Rectangle{
    width: 500
    height: 500
    color: "dark grey"


    Rectangle {
        id: backgroundRect
        width: 200
        height: 150
        radius: 5
        anchors.centerIn: parent
        color: "red"

        Rectangle {
            id: dropShadowRect
            property real offset: Math.min(parent.width*0.025, parent.height*0.025)
            color: "purple"
            width: parent.width
            height: parent.height
            z: -1
            opacity: 0.75
            radius: backgroundRect.radius + 2
            anchors.left: parent.left
            anchors.leftMargin: -offset
            anchors.top: parent.top
            anchors.topMargin: offset
        }
    }
}
于 2013-03-19T00:09:12.970 回答
0

我尝试了上面的代码,它实际上添加了一个阴影,尽管在我的例子中,简单地添加另一个带有一点偏移的矩形给了我一个我更喜欢的效果。

Rectangle{

    id: rec_Shadow
    height:rect_withShadow.height
    width: rect_withShadow.width
    border.color: "#B3B3B3"
    color: "#C5C5C5"

    anchors{
            verticalCenter: rect_withShadow.verticalCenter
            horizontalCenter:  rect_withShadow.horizontalCenter
            horizontalCenterOffset: 5
            verticalCenterOffset: 5
        }
    radius: rect_withShadow.radius
}

接下来,添加要在其上添加阴影的 Rectangle,并将其命名为 rect_withShadow

于 2015-03-14T06:58:27.607 回答