properties - 如何将 QML 项目分配给 QML 中的组件属性,然后在组件内使用该对象?

标签 properties qml

我正在尝试创建一个 QML 对象,它就像其他对象的包装器一样。这是我的 QML 文件(Container.qml):

Item {
    property string label
    property Item control

    Row {
        Label {
            text: label
        }

        // Not sure how to display the control assigned to the control property
    }
}

我想做的(在我使用这个组件的 QML 中)是这样的:
Container {
    label: "My Label"
    control: Textbox {
        text: "My Value"
    }
}

当输入该 QML 时,结果(在界面中)应该类似于此 QML 的输出:
Item {
    Row {
        Label {
            text: "My Label"
        }
        Textbox {
            text: "My Value"
        }
    }
}

这可能吗?当我尝试这样做时,在将项目分配给控件属性时,我得到“无法将对象分配给属性”。我搜索了 Qt 论坛并无情地用谷歌搜索,但没有成功。如果有人知道答案,将不胜感激。

谢谢

jack

最佳答案

有更好的解决方案:

/* MyObject.qml */

Rectangle {
    default property alias data /* name can be any */ : inner_space.data

    /* ... You can put other elements here ... */
    Item {
       id: inner_space

       /* ... Params ... */
    }
    /* ... You can put other elements here ... */
}

现在我们可以做我们想做的一切了!
/* main.qml */

Rectangle {
    MyObject {
        Button {
             /* ... */
        }
    }
}

感谢用户bobbaluba建议使用 data属性而不是 children .

关于properties - 如何将 QML 项目分配给 QML 中的组件属性,然后在组件内使用该对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021350/

相关文章:

c# - NHibernate QueryOver 用投影填充父对象中的属性对象

qt - 有没有办法在 Qt5 qml 中执行 C++ 宏?

c++ - QML 如何动态访问 ListView 项

qt - QML:如何创建一个由图像制成的三态自定义按钮?

c++ - 从 QML 动态创建 C++ 对象

html - css object-fit 属性有什么问题

c# - 什么是 C# 中的公共(public)获取/保护集属性的 Objective-C 等价物

java - 如何读取Spring Boot application.properties?

java - 读取conf文件夹内的属性文件

javascript - 在 QML 中加载页面时运行 javascript 函数