代码之家  ›  专栏  ›  技术社区  ›  Fredro

QML文本阴影

  •  1
  • Fredro  · 技术社区  · 7 年前

    在QML中,有没有合适的方法将外部阴影添加到文本对象?我尝试使用DropShadow,但它用黑色填充整个文本字段。

    Text {
        id: textId
        font.pixelSize: 36
        font.letterSpacing: 0.9
        color: "red"
        text: "Hello World"
    
        DropShadow {
            anchors.fill: parent
            verticalOffset: 2
            color: "#80000000"
            radius: 1
            samples: 3
        }
    }
    

    我也试着更换锚。填充源代码并设置文本id。同时在文本对象外部使用DropShadow。没有阴影。

    text-shadow: 0 2px 4px
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Mitch    7 年前

    最简单的方法是使用 item layers :

    import QtQuick 2.9
    import QtQuick.Window 2.3
    import QtGraphicalEffects 1.0
    
    Window {
        id: window
        width: 800
        height: 600
        visible: true
    
        Text {
            id: textId
            font.pixelSize: 36
            font.letterSpacing: 0.9
            color: "red"
            text: "Hello World"
    
            layer.enabled: true
            layer.effect: DropShadow {
                verticalOffset: 2
                color: "#80000000"
                radius: 1
                samples: 3
            }
        }
    }
    

    你也可以像在 DropShadow docs 滤镜

    import QtQuick 2.9
    import QtQuick.Window 2.3
    import QtGraphicalEffects 1.0
    
    Window {
        id: window
        width: 800
        height: 600
        visible: true
    
        Text {
            id: textId
            font.pixelSize: 36
            font.letterSpacing: 0.9
            color: "red"
            text: "Hello World"
        }
    
        DropShadow {
            anchors.fill: textId
            source: textId
            verticalOffset: 2
            color: "#80000000"
            radius: 1
            samples: 3
        }
    }
    

    你错过了 source 但如果您尝试将阴影作为 Text :