【问题标题】:How to prevent blurry QtGraphicalEffects from being cut off?如何防止模糊的 QtGraphicalEffects 被截断?
【发布时间】:2023-04-08 21:34:01
【问题描述】:

我正在尝试使用带有 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
    }
}

【问题讨论】:

  • 你试过smooth : true 吗?
  • smooth: true 没有任何明显的区别。至少,不会到截止边缘。

标签: qt qml qt-quick


【解决方案1】:

您必须允许原始项目(将被效果复制)周围有足够的空间以允许完全绘制效果,我会这样做:

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 是我见过的唯一可行的解​​决方案。
  • @Slavik81:你不会找到任何更好的解决方案,因为问题来自 Qt 本身,图形效果是通过在项目上应用 ShaderEffect 来完成的(这基本上是QML 2.0 中的 OpenGL 纹理),并且此 ShaderEffect 不能在它正在复制的项目的纹理之外绘制。也许您应该在 QT-BUG 服务器上提交功能请求/错误报告;-)
【解决方案2】:

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

【讨论】:

  • 谢谢。我开始怀疑这就是原因。但我仍然不知道如何解决这个问题,更不用说如何干净地做到这一点了。你能详细说明一下吗?
  • 很遗憾,我从未使用过 QML,因此无法详细说明。我曾经对 Qt Graphics View 框架有同样的问题,解决方法是计算(放大)项目的边界矩形,这样效果就不再被切断了。尝试将 Text 项目大小强制为大,看看它是否改变了一些东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 2021-06-17
  • 2020-06-29
  • 2013-02-16
  • 2016-12-28
相关资源
最近更新 更多