首页 > 解决方案 > DropShadow 的自定义组件

问题描述

由于这些属性DropShadow不变:

radius: 10
samples: 15
color: "black"

在我的使用中,我尝试创建一个名为的自定义组件Shadow.qml来设置这些:

anchors.fill: target
source: target

两个属性。这里是:

import QtQuick 2.0
import QtGraphicalEffects 1.0

DropShadow{
    id: root
    /*
    property string target
    anchors.fill: target
    source: target
    */

    //property alias target: root.anchors.fill | root.source

    /*
    property alias fill: root.anchors.fill
    property alias source: root.source
    */

    radius: 10
    samples: 15
    color: "black"
}

在我的使用中,我可以这样使用它:

Shadow{
    anchors.fill: myTarget
    source: myTarget
}

它有效。问题是我必须myTarget在两个地方(即anchors.fillsource)写,所以我想把它减少到像这样的单行Shadow{ target: myTarget }

我已经尝试了这 3 个被注释掉的部分中的每一个,Shadow.qml但这些都不起作用!

标签: qtqmlcustom-componentdropshadow

解决方案


目标不应该是string它应该是应用 DropShadow 的对象,它可以是任何类型的图形Item

property Item target
anchors.fill: target
source: target

应该管用。
但是,您也可以让它填充源:

anchors.fill: source

现在,不是设置目标,而是设置源:

Shadow {
    source: Rectangle { ... }
}

如果需要属性名称target,请为源创建别名:

DropShadow {
    property alias target: source
    anchors.fill: source
    ...
}

创建别名而不是真实属性应该更有效。


推荐阅读