qt - 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.fill
和source
)写,所以我想把它减少到像这样的单行Shadow{ target: myTarget }
我已经尝试了这 3 个被注释掉的部分中的每一个,Shadow.qml
但这些都不起作用!
解决方案
目标不应该是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
...
}
创建别名而不是真实属性应该更有效。
推荐阅读
- jquery - 如何使用计时器从 elemant 中提取文本?
- c# - 如何在uwp中保存全屏墨水文件?
- javascript - 网络推送通知可以在点击时启动浏览器吗?
- python - 使用 Python 在 Mac OS 上与 docker 内的服务器的 TCP 连接上客户端的管道损坏
- reactjs - 如何在带有 create-react-app 的 url 中使用域名而不是 'localhost:3000' 进行本地开发?
- ubuntu - resolv.conf 名称服务器响应的顺序
- ruby-on-rails - 如何更改 Rails 管理员查询?
- python - 在 numpy 数组(或任何数组)的切片中查找最小值的基本帮助?
- python - pandas ValueError:只能比较标签相同的系列对象python
- android - 由于 recyclerview 和 cursorloader 导致的空白主要活动