c++ - 公开子属性时出现 QML 错误

标签 c++ qt qml

我有一个定义如下的 QML 对象:

Item {
    property alias source: a.internalImage.source
    property alias text: a.internalText.text

    Column {
        id: a

        Image {
            id: internalImage
        }

        Text {
            id: internalText
            width: internalImage.width
        }
    }
}

这失败了:无效的别名目标位置:internalImage

但是,如果我这样做:

Column {
    property alias source: internalImage.source
    property alias text: internalText.text

    Image {
        id: internalImage
    }

    Text {
        id: internalText
        width: internalImage.width
    }
}

这是为什么?

最佳答案

来自 documentation , Component 的范围是:

the union of the object ids within the component and the component's root element's properties

因此,不允许外部元素通过id链访问内部元素中包含的id
另一方面,如果您通过一组变量显式导出一组参数,则这些值/引用可免费供外部组件使用。

关于c++ - 公开子属性时出现 QML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803584/

相关文章:

c++ - 在多态对象上使用 typeid 时,必须定义它吗?

c++ - 自定义类型 Qlist 和范围

c++ - 专业环境中的 Qt

python - 我可以将从 pyuic 生成的 python 代码转换回 ui 文件吗?

qml - 手动更新/重新绘制项目

c++ - 链表 C++ 删除节点

c++ - 创建其中包含透明 "window"的像素图,由另一个像素图合成

linux - QThread::start(priority) 与 Linux

qml - 为什么我的 QML 属性在绑定(bind)到 Slider.value 属性时不更新?

qt - QML跨平台设计问题: text sizes