【问题标题】:How to get value from a property in Qml from another Qml?如何从另一个 Qml 的 Qml 中的属性中获取值?
【发布时间】:2017-10-05 16:00:35
【问题描述】:

我知道这个问题被问了很多次,但没有一个答案可以帮助我。我有两个 QML 文件。第一个 Qml 文件中有一个地图,当我按下地图时,它会打开一个窗口,其中包含以下代码:

MouseArea{
    id: mouseArea
    property var positionRoot: map.toCoordinate(Qt.point(mouseX, mouseY))
    anchors.fill: parent
    onClicked: {
            var component = Qt.createComponent("addAttribute.qml")
            if (component.status === Component.Ready) {
            var dialog = component.createObject(parent,{popupType: 1})
            dialog.show()
       }
    }
}

窗口有一个labeltextfield和一个button。我想要的是获取我在第一个 QML 文件中创建的 property(positionRoot) 的值。我怎么可能做到?

【问题讨论】:

  • 在第二个窗口中创建与positionRoot相同类型的属性,并在创建时传递值-dialog.positionRoot2 = positionRoot;

标签: qt qml


【解决方案1】:

感谢@folibis,它成功了。这是需要添加的代码。

MouseArea{
        id: mouseArea
        property var positionRoot: map.toCoordinate(Qt.point(mouseX, mouseY))
        anchors.fill: parent
        onClicked: {
                var component = Qt.createComponent("addAttribute.qml")
                if (component.status === Component.Ready) {
                var dialog = component.createObject(parent,{popupType: 1})
                dialog.sqlPosition = positionRoot
                dialog.show()
           }
        }
    }

在我的第二个窗口中,我只是创建了一个名为 sqlPosition 的新属性。例如:

Window {
        id: secondWindow
        property var sqlPosition
    }

【讨论】:

    猜你喜欢
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2019-04-13
    • 2021-12-28
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多