如何访问加载器对象内部的项目属性 [英] how to access properties of item inside loader object

查看:115
本文介绍了如何访问加载器对象内部的项目属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用qml Loader组件将页面动态推送到视图中.现在,我推送的页面具有各种属性,我想从Loader组件本身访问它们.但是,我无法为这些属性创建别名.所以,我有类似的东西:

I am using a qml Loader component to push a page dynamically into view. Now the page I push has various properties and I would like to access them from the Loader component itself. However, I am unable to create aliases to these properties. So, I have something like:

Loader {
    id: loginLoader
    source: "qrc:/pages/IdPinLoginPage.qml"

    property alias hasNavBar: loginLoader.item.hasNavBar
    property alias hasStatusBar: loginLoader.item.hasStatusBar
}

这将导致Invalid alias target location.如何将这些属性重定向到Loader组件中的父级?

This results in Invalid alias target location. How can I redirect these properties to the parent level in the Loader component?

推荐答案

我建议您使用sourceComponent而不是Loader的source属性来轻松地将值绑定/绑定到已加载的项目.
同样对于访问已加载的项目,您可以使用加载程序的item属性:

I recommended you to use sourceComponent instead of source property of the Loader for assign/bind value to loaded item easily.
Also for access loaded item, you can use item property of the loader:

Loader {
    id: loader
    sourceComponent: Component {
        SampleItem {
            foo: 13         //assign values to loaded item properties
            bar: "aleyk!"
        }
    }
}

Text {
    text: loader.item.bar   //Access to loaded item properties
}

SampleItem.qml

import QtQuick 2.6

Item {
    property int foo: 0
    property string bar: "salam";
}

这篇关于如何访问加载器对象内部的项目属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆