OpacityMask的作用是使用另一项屏蔽源项,具体示例代码如下:
//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
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();
}
//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.12
Window {
visible: true
width: 800
height: 700
title: qsTr("Hello World")
Image {
id: bug1
source: "images/bug.jpg"
sourceSize: Qt.size(300, 300)
smooth: true
}
Image {
id: mask1
anchors.top: parent.top
anchors.right: parent.right
source: "images/butterfly.png"
sourceSize: Qt.size(300, 300)
smooth: true
}
Item {
width: 300
height: 300
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
Image {
id: bug
source: "images/bug.jpg"
sourceSize: Qt.size(parent.width, parent.height)
smooth: true
visible: false
}
Image {
id: mask
source: "images/butterfly.png"
sourceSize: Qt.size(parent.width, parent.height)
smooth: true
visible: false
}
OpacityMask {
anchors.fill: bug
source: bug
maskSource: mask
}
}
}
运行效果: