“通过引用”将属性传递给组件

时间:2020-10-07 11:10:56

标签: qt qml qt5 qtquick2

我想制作一个可以“通过引用”更改另一个组件属性的组件。

例如:

TextField {
    id: lat
    text: MyDataSource.latitude
}

LatLongButton {
    latitude: lat.text
    // or:
    // latitude: MyDataSource.latitude
}

例如,当用户单击按钮时,我想更新lat.text的值。现在的问题是,当我在单独文件中将LatLongButton创建为组件时:

LatLongButton {
    // doesn't work, alias has to be set initially to something
    // property alias latitude: ???
    // overriding it in the main file is also not possible

    property real latitude: 0
    onClicked: {
        // I want this to change some other property indirectly
        // that is, one that I specify in the main file
        latitude = 123
        // but this here doesn't set the value through the binding -
        // it seems to destroy the binding and set the value of this
        // component directly
    }
}

有没有办法做到这一点?我认为一定有,因为诸如TextField之类的内置小部件的工作方式是这样的。用户进行交互,然后他们可以跨绑定更改属性

0 个答案:

没有答案