Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. I want to dynamically attach any Component to layer.effect
QtWS25 Last Chance

I want to dynamically attach any Component to layer.effect

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 264 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ynakane
    wrote on last edited by
    #1

    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)]
    dropshadow.png
    [Horizontal Offset:10 (I want to achieve)]
    dropshadow_hoffset10.png

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved