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

≯℡__Kan透↙ 提交于 2019-12-20 20:29:32

问题


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 the property x of the object inside this Loader's sourceComponent?

 import QtQuick 2.0

 Item {
     width: 200; height: 200

     Loader {
         anchors.fill: parent
         sourceComponent: rect
     }

     Component {
         id: rect
         Rectangle 
         {
             width: 50
             height: 50
             color: "red"
             property int x
         }
     }
 }

回答1:


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
         }
     }
 }


来源:https://stackoverflow.com/questions/28360339/how-to-access-the-qml-object-inside-the-loaders-sourcecomponent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!