如何防止模糊的QtGraphicalEffects被切断?

时间:2013-01-29 05:56:36

标签: qt qml qt-quick

qml window with cut-off drop shadow

我正在尝试使用带有QtGraphicalEffects的QtQuick 2为我的项目添加效果,但我不太明白如何调整真正模糊的效果以使其看起来正确。

在这种情况下,投影的大小很差,并且在完全消失之前会在边缘处被剪裁。我怎样才能让它很好地混合而不被切断?

这是窗口的代码:

import QtQuick 2.0
import QtGraphicalEffects 1.0

Item {
    width: 250
    height: 75

    Text {
        id: textItem
        x: 10; y: 10
        text: "how can I fix my shadow?"
    }

    DropShadow {
        id: shadowEffect
        anchors.fill: textItem
        source: textItem

        radius: 8
        samples: 16

        horizontalOffset: 20
        verticalOffset: 20
    }
}

2 个答案:

答案 0 :(得分:8)

你必须允许原始项目(将由效果复制)在其周围留出足够的空间以允许完全绘制效果,我会这样做:

import QtQuick 2.0
import QtGraphicalEffects 1.0

Item {
    width: 320;
    height: 240;

    Text {
        id: textItem;
        anchors.centerIn: parent;
        text: "how can I fix my shadow?";

        /* extend the bounding rect to make room for the shadow */
        height: paintedHeight + (shadowEffect.radius * 2);
        width: paintedWidth + (shadowEffect.radius * 2);

        /* center the text to have space on each side of the characters */
        horizontalAlignment: Text.AlignHCenter;
        verticalAlignment: Text.AlignVCenter;

        /* hide the original item because the Graphical Effect duplicates it anyway */
        visible: false;
    }
    DropShadow {
        id: shadowEffect;
        anchors.fill: source;
        source: textItem;
        radius: 8;
        samples: 16;
        horizontalOffset: 20;
        verticalOffset: 20;
    }
}

答案 1 :(得分:2)

Qt图形效果绑定到它们适用的项目的边界矩形。使边界矩形足够大(可能使用最小尺寸或任何丑陋的解决方案),所以你没有那种“切断”的外观