代码之家  ›  专栏  ›  技术社区  ›  Antoine Morrier

QML别名:无法使用层找到id

  •  0
  • Antoine Morrier  · 技术社区  · 6 年前

    我正在尝试创建ShaderEffect的别名。但是,当我这样做时:

    Item {
        id: root;
        property color colorBackground: Qt.rgba(0.9, 0.9, 0.9, 1.0);
        property color colorStroke: Qt.rgba(0.1, 0.1, 0.1, 1.0);
        layer.enabled: true;
    
        layer.effect: ShaderEffect {
            property alias colorBackground: root.colorBackground;
            property alias colorStroke: root.colorStroke;
    
            fragmentShader: "
                uniform lowp sampler2D source; // This item
                uniform lowp float qt_Opacity; // Inherited opacity of this item
                uniform lowp vec4 colorBackground;
                varying highp vec2 qt_TexCoord0;
                void main() {
                    lowp vec4 p = texture2D(source, qt_TexCoord0);
                    lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
                    gl_FragColor = colorBackground;
                }";
        }
    }
    

    Unable to find id root

    当我这么做的时候:

    Item {
        property alias colorBackground: shaderEffect.colorBackground;
        property alias colorStroke: shaderEffect.colorStroke;
        layer.enabled: true;
    
        layer.effect: ShaderEffect {
            id: shaderEffect;
            property color colorBackground: Qt.rgba(0.9, 0.9, 0.9, 1.0);
            property color colorStroke: Qt.rgba(0.1, 0.1, 0.1, 1.0);
            fragmentShader: "
                uniform lowp sampler2D source; // This item
                uniform lowp float qt_Opacity; // Inherited opacity of this item
                uniform lowp vec4 color;
                varying highp vec2 qt_TexCoord0;
                void main() {
                    lowp vec4 p = texture2D(source, qt_TexCoord0);
                    lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
                    gl_FragColor = color;
                }";
        }
    }
    

    我得到了同样的错误,但与shaderEffect。

    如何解决这个问题?

    2 回复  |  直到 6 年前
        1
  •  1
  •   dtech    6 年前

    layer.effect 真的期望 Component

    由于没有具体的对象实例,因此没有 id

    以及 alias

    工作原理:

    Item {
      id: root;
      property color colorBackground: Qt.rgba(0.9, 0.9, 0.9, 1.0);
      property color colorStroke: Qt.rgba(0.1, 0.1, 0.1, 1.0);
      layer.enabled: true;
    
      layer.effect: ShaderEffect {
        property color colorBackground: root.colorBackground;
        property color colorStroke: root.colorStroke;
    
        fragmentShader: "
                uniform lowp sampler2D source; // this item
                uniform lowp float qt_Opacity; // inherited opacity of this item
                uniform lowp vec4 colorBackground;
                varying highp vec2 qt_TexCoord0;
                void main() {
                    lowp vec4 p = texture2D(source, qt_TexCoord0);
                    lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
                    gl_FragColor = colorBackground;
                }";
      }
    }
    
        2
  •  1
  •   derM - not here for BOT dreams    6 年前

    layer.effect is of type Component .

    Components build their own scope root 仅通过动态作用域可用。

    According to the documentation of property aliases:

    • 它只能引用在声明别名的类型范围内的对象或对象的属性。
    • 它不能引用在其类型范围之外声明的对象。

    因此,它必须失败。