首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用qt组件OpacityMask

如何使用qt组件OpacityMask
EN

Stack Overflow用户
提问于 2017-12-20 05:30:14
回答 1查看 1.3K关注 0票数 0

有一个问题是如何在QML中使用透明矩形隐藏图像?

公认的答案是使用OpacityMask。

我按照这个答案创建了一个qml文件,但是没有得到预期的结果。

我的密码有什么问题吗?

代码语言:javascript
运行
复制
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtGraphicalEffects 1.0

ApplicationWindow {
    visible: true
    width: 1280
    height: 800
    title: qsTr("Hello World")

    Rectangle {
        id: background
        anchors.fill: parent
        color: "black"
    }

    Image
    {
        id:underlyingImage
        width: 1204
        height: 682

        fillMode: Image.PreserveAspectCrop
        layer.enabled: true
        layer.effect: OpacityMask {
            maskSource: hiding_rect
        }

        source:"qrc:/timg.jpg"
    }
    Rectangle
    {
        id:hiding_rect
        width: underlyingImage.width/2
        height: underlyingImage.height/2
        color: "yellow"
    }
}

结果图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-21 11:20:57

我不太熟悉另一个问题中提出的方法。但是,按照文档(http://doc.qt.io/qt-5/qml-qtgraphicaleffects-opacitymask.html)中建议的方法,这是可行的:

代码语言:javascript
运行
复制
Window {
    visible: true
    width: 1280
    height: 800
    title: qsTr("Hello World")

    Rectangle {
        id: background
        anchors.fill: parent
        color: "black"
    }

    Image
    {

        anchors.centerIn: parent

        id:underlyingImage
        width: 1204
        height: 682

        fillMode: Image.PreserveAspectCrop
        source:"file:///tmp/timg.jpg"
        visible: false
    }

    Item {
        id:hiding_rect
        anchors.fill: underlyingImage
        visible: false
        Rectangle
        {
            anchors.left: parent.left
            anchors.top: parent.top
            width: underlyingImage.width/2
            height: underlyingImage.height/2
            color: Qt.rgba(1,1,1,1)
            z: underlyingImage.z + 1
        }
    }




    OpacityMask {
        anchors.fill: underlyingImage
        source: underlyingImage
        maskSource: hiding_rect
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47899361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档