qt圆形图像剪切

这篇博客展示了如何在QML中结合Rectangle和OpacityMask组件,以及配合C++代码,创建一个图像剪切效果。通过设置Rectangle的半径和OpacityMask的属性,实现了圆形图像显示。QML的代码详细描述了组件的布局和属性设置,而C++部分则包含了应用的入口和QML文件的加载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 剪切代码:



Rectangle{
                    id:idRectRound
                    width: 250
                    height: 250
                    radius: width/2
                    anchors.centerIn: parent
                    color: "#ff00ff"
                    visible: false
                }
                Image {
                    id: idRectImg
                    width: 250
                    height: 250
                    anchors.centerIn: parent
                    source: "qrc:/res/demo.png"
                    visible: false
                    smooth: true
                }
                OpacityMask {
                    anchors.fill: idRectRound
                    source: idRectImg
                    maskSource: idRectRound
               }

完整QML源码

import QtQuick 2.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    color:"black"
    Rectangle{
                        id:idRectRound
                        width: 250
                        height: 250
                        radius: width/2
                        anchors.centerIn: parent
                        color: "#ff00ff"
                        visible: false
                        border.color: "yellow"
                        border.width: 2
                    }
                    Image {
                        id: idRectImg
                        anchors.centerIn: parent
                        source: "qrc:/res/demo.png"
                        visible: false
                        smooth: true
                    }
                    OpacityMask {
                        anchors.fill: idRectRound
                        source: idRectImg
                        maskSource: idRectRound
                   }
}

C++代码:

#include <QGuiApplication>
#include <QQmlApplicationEngine>


int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值