I want to dynamically attach any Component to layer.effect
-
Hi.
I want to dynamically add a shadow to QML
I want to achieve multi-sampling, so I set layer.enabled = true
Therefore, when creating a DropShadow object as a sibling of the Shadow target, I found that the Shadow was clipped to the size of the item (Rectangle in the example).
As a way to avoid this, I noticed that I could attach DropShadow Component to layer.effect, so I dynamically attached the Component to layer.effect.
So far so good, but when I tried to change the Shadow's properties (horizontalOffset, for example), I couldn't figure out how to specify them since Qt.CreateComponent() method does not pass the property as argument.This is a snippet of my code.
Does anyone knows this matter.[main.qml]
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") property var shadowComponent: null Rectangle{ id: rect anchors.centerIn: parent width: 200 height: 100 color: "blue" layer.enabled: true layer.samples: 8 MouseArea{ anchors.fill: parent onClicked: { createShadow() } } } function createShadow(){ if(shadowComponent) { shadowComponent.destroy() shadowComponent = null rect.layer.effect = null } shadowComponent = Qt.createComponent("MyDropShadow.qml") // shadowComponent.horizontalOffset = 10 ← Can not pass property like this cause this is not object but component if(shadowComponent) { rect.layer.effect = shadowComponent } } }
[MyDropShadow.qml]
import QtQuick 2.0 import QtGraphicalEffects 1.0 DropShadow { horizontalOffset: 3 verticalOffset: 3 radius: 8.0 samples: 17 color: "#80000000" }
[Horizontal Offset:3 (default)]
[Horizontal Offset:10 (I want to achieve)]