Qt Quick里的图形效果 阴影(Drop Shadow)

本文详细介绍了Qt Quick中的DropShadow和InnerShadow两种阴影效果,包括它们的特点、使用方法及源码分析。通过示例展示了阴影效果,并提供了对应的QML代码,帮助理解如何在Qt应用中实现阴影效果。

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

               

    Qt Quick提供了两种阴影效果:

  • DropShow,阴影。这个元素会根据源图像,产生一个彩色的、模糊的新图像,把这个新图像放在源图像后面,给人一种源图像从背景上凸出来的效果。
  • InnerShadow,内阴影。这个元素会根据源图像,产生一个彩色的、模糊的新图像,与 DropShadow不同的是,新图像会放在源图像里面。

效果

    下面是我设计的示例效果。

    首先是 DropShadow :


                       图1 阴影效果

    然后是内阴影效果:


                    图2 内阴影效果

源码分析

    如图1所示,界面被分为三部分。

    最上面的是源图像。

    源图像下面(即中间)是一个列表,你可以点击 DropShadow 和 InnerShadow 两个子项,切换不同的阴影效果。每种阴影效果都对应一个 qml 文档,当你点击这些子项时,对应的 qml 文档动态加载。

阴影示例界面

    这个示例界面框架其实与“Qt Quick里的图形效果——颜色(Color)”是一致的,只是我把 ListView 从原来的竖向改为了横向。对应的 DropShadowExample.qml 内容如下:

import QtQuick 2.2import QtQuick.Controls 1.2Rectangle {    id: example;    signal back();    anchors.fill: parent;    Text {        id: origLabel;        x: 10;        y: 4;        font.pointSize: 20;        text: "Original Image";    }    Button {        anchors.right: parent.right;        anchors.top: parent.top;        anchors.margins: 4;        text: "Back";        onClicked: example.back();    }    Image {        id: origImage;        width: 240;        height: 240;        anchors.left: parent.left;        anchors.top: origLabel.bottom;        anchors.margins: 4;        source: "butterfly.png";        sourceSize: Qt.size(240, 240);        smooth: true;    }    Rectangle{        anchors.left: parent.left;        anchors.leftMargin: 4;        anchors.right: parent.right;        anchors.rightMargin: 4;        anchors.top: origImage.bottom;        height: 2;        border.width: 1;        border.color: "darkgray";    }    Text {        id: effectsLabel;        anchors.top: origImage.bottom;        anchors.margins: 4;        anchors.left: parent.left;        font.pointSize: 20;        font.bold: true;        text: "Shadow Effects:";        color: "blue";    }    Rectangle {        id: shadowEffects;        anchors.left: effectsLabel.right;        anchors.leftMargin: 4;        anchors.top: effectsLabel.top;        anchors.right: parent.right;        anchors.rightMargin: 4;        height: 40;        color: "gray";        ListView {            anchors.fill: parent;            clip: true;            focus: true;            orientation: ListView.Horizontal;            spacing: 20;            delegate: Text {                id: wrapper;                height: 40;                verticalAlignment: Text.AlignVCenter;                text: name;                font.pointSize: 18;                Keys.onEnterPressed: {                    event.accepted = true;                    effectControl.source = example;                }                Keys.onReturnPressed: {                    event.accepted = true;                    effectControl.source = example;                }                MouseArea {                    anchors.fill: parent;                    onClicked: {                        wrapper.ListView.view.currentIndex = index;                        effectControl.source = example;                    }                }            }            highlight: Rectangle {     
以下是使用PropertyAnimation实现鼠标进入Rectangle时出现渐变阴影的代码: ``` Rectangle { id: rect width: 200 height: 200 color: "lightblue" property real shadowRadius: 0 MouseArea { anchors.fill: parent hoverEnabled: true onEntered: { var anim = Qt.createQmlObject('import QtQuick 2.0; PropertyAnimation {target: rect; property: "shadowRadius"; to: 20; duration: 200}', rect) anim.start() } onExited: { var anim = Qt.createQmlObject('import QtQuick 2.0; PropertyAnimation {target: rect; property: "shadowRadius"; to: 0; duration: 200}', rect) anim.start() } } Rectangle { anchors.fill: parent color: "transparent" border.color: "lightgray" border.width: 1 radius: 5 opacity: 0.5 // Use the shadowRadius property to create a drop shadow layer.enabled: true layer.effect: DropShadow { radius: rect.shadowRadius color: "black" samples: 16 horizontalOffset: 0 verticalOffset: 0 } } } ``` 在这个代码中,我们首先定义了一个Rectangle,其中包含了一个MouseArea,负责处理鼠标进入/离开事件。当鼠标进入时,我们使用Qt.createQmlObject创建了一个PropertyAnimation,将Rectangle的shadowRadius属性从0变为20,实现了渐变阴影效果;当鼠标离开时,我们同样创建了一个PropertyAnimation,将shadowRadius属性从20变为0,恢复到没有阴影的状态。 为了实现阴影效果,我们在Rectangle内部添加了另一个Rectangle,用于绘制阴影。这个Rectangle的颜色是透明的,但是使用了DropShadow效果,通过调整radius属性实现阴影的大小和模糊度。注意,我们使用了shadowRadius属性来控制DropShadow的radius属性,以便在PropertyAnimation中动态改变阴影大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值