2017-12-18 236 views
0

有一个QML文件中像这样的特定区域:如何使QML视图的透明

Item { 
    width: 800 
    height: 600 

    Image { 
     id: background 
     width: 800 
     height: 600 
     source: "qrc:/resorces/background.png" 
    } 

    Rectangle { 
     id: transframe 
     x: 500 
     y: 200 
     width: 200 
     height: 100 
    } 

} 

如何使transframe透明的区域,然后我可以看到背景下的图形。

+0

这将是有益的,如果你可以添加你都注意到了什么的截图。也许你可以设置颜色:“透明”属性对于变换帧 –

回答

1

OpacityMask是你在找什么。

例子:

Rectangle { 
     width: 800; height: 600 
     color: 'red' 

     Image { 
      id: background 
      width: 800; height: 600 
      source: "qrc:/resorces/background.png" 
      visible: false 
     } 
     Item { 
      id: transframe 
      width: 800; height: 600 
      visible: false 
      Rectangle { 
       x: 500; y: 200; width: 200; height: 100 
      } 
     } 
     OpacityMask { // don't forget to import QtGraphicalEffects 
      anchors.fill: background 
      source: background 
      maskSource: transframe 
      invert: true 
     } 
    } 
+1

尽管此链接可能回答此问题,但最好在此处包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/18287446) –

+0

非常感谢您的回答,OpacityMask就是我要找的。 –

+0

@Andrii我正在使用Qt 5.6,但是在Qt 5.7中引入了invert的属性。我认为在Qt 5.6中将maskSource作为除transframe之外的区域可能很有用。有没有其他方法? –

-1
Item { 
    width: 800 
    height: 600 
    Image { 
     id: background 
     width: 800 
     height: 600 
     source: "qrc:/resorces/background.png" 
    } 
    Rectangle { 
     id: transframe 
     x: 500 
     y: 200 
     width: 200 
     height: 100 
     color:"transparent" 
    } 
} 
+0

可以是透明的,但是背景中的变换帧区域仍然不透明。我仍然无法在背景下看到图形。 –