Qt_Note20_QML_自定义Grid控件与OpacityMask的使用

cpp 复制代码
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.14

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    // 自定义Grid控件与OpacityMask的使用
    Grid {
        id: grid
        width: 15
        height: 200
        columns: 3
        x: 300
        y: 100
        visible: false  //看不到了
        Repeater {
//            model: grid.width / 5 * grid.height / 5
            model: grid.width / 5 * 200 / 5
            Rectangle {
                width: 5
                height: 5
                color: index % 2 ? "black" : "white"
                Component.onCompleted: {
                    console.log("rect" + index)
                }
            }
        }
    }

    Rectangle {
        id: maskRect
        x: 200
        y: 100
        width: grid.width
        height: grid.height
        visible: true
        radius: 10
        border.color: "red"
    }

    Button {
        width: 50
        height: 50
        x: 100
        y: 100
        onClicked: {
            grid.height -= 10
        }
    }

    Rectangle{
        width: grid.width + 4
        height: grid.height + 4
//        anchors.centerIn: parent
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        border.color: "red"
        radius: 10
        border.width: 3
        OpacityMask {
            source: grid
            maskSource: maskRect
            anchors.fill: parent
            anchors.margins: 2
        }
    }


}
相关推荐
Moment3 分钟前
AI 全栈指南:NestJs 中的 Service Provider 和 Module
前端·后端·面试
IT_陈寒6 分钟前
为什么我的JavaScript异步回调总是乱序执行?
前端·人工智能·后端
Moment9 分钟前
AI全栈入门指南:NestJs 中的 DTO 和数据校验
前端·后端·面试
cmpxr_16 分钟前
【C】局部变量和全局变量及同名情况
c语言·开发语言
小码哥_常20 分钟前
告别RecyclerView卡顿!8个优化技巧让列表丝滑如德芙
前端
小村儿30 分钟前
Harness Engineering:为什么你用 AI 越用越累?
前端·后端·ai编程
hetao173383741 分钟前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
6Hzlia43 分钟前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
enoughisenough1 小时前
浏览器判断控制台是否开启
前端
小碗羊肉1 小时前
【从零开始学Java | 第三十一篇下】Stream流
java·开发语言