本文介绍了如何访问 Loader 的 sourceComponent 中的 QML 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能需要从某个外部函数读取或写入 LoadersourceComponent 的某些属性.

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

如何访问这个LoadersourceComponent中对象的属性x?

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

推荐答案

当您需要向外部公开内部对象/属性时,您应该创建一个 别名.

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

这篇关于如何访问 Loader 的 sourceComponent 中的 QML 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:57
查看更多