How to access the QML object inside the Loader's sourceComponent?

前端 未结 1 2025
我寻月下人不归
我寻月下人不归 2021-02-07 21:00

I may need to read or write to some of the properties of the Loader\'s sourceComponent from some outside function.

What is the way to access th

相关标签:
1条回答
  • 2021-02-07 21:15

    When you need to expose an inner object/property to the outside, you should create an alias to it.

    import QtQuick 2.0
    
     Item {
         width: 200; height: 200
         property alias loaderItem: loader.item
    
         Loader {
             id: loader
             anchors.fill: parent
             sourceComponent: rect
         }
    
         Component {
             id: rect
             Rectangle 
             {
                 width: 50
                 height: 50
                 color: "red"
                 property int x
             }
         }
     }
    
    0 讨论(0)
提交回复
热议问题