我有兩個 QML 檔案如下:
//Page.qml
SelectionPage {
model: localizationPageProxy.vehicleTypes //QObject* class exposed by property
currentKey: localizationPageProxy.vehicleTypes.currentDataKey //QVariant property
}
//SelectionPage.qml
Item {
property var model
property var currentKey
id: page
ColumnLayout {
height: parent.height
width: parent.width * 0.9
anchors.horizontalCenter: parent.horizontalCenter
ListView {
id: listView
anchors.fill: parent
ScrollBar.vertical: ScrollBar {}
clip: true
model: page.model.data
spacing: Number.EPSILON // I don't know why the data loading is faster with that
delegate: Item {
height: listView.height * 0.12
width: listView.width
RadioButtonItem {
height: parent.height * 0.85
width: parent.width
anchors.centerIn: parent
text: modelData.value
checked: modelData.key === page.currentKey
onClicked: page.currentKey = modelData.key //here the c property is changed
}
}
}
}
}
那么,SelectionPage.qml 的 currentKey 屬性是通過參考傳遞的嗎?
如果那是副本,我不應該看到 c 模型發生變化。
謝謝您的幫助
uj5u.com熱心網友回復:
與其討論復制與參考,不如討論系結。當你這樣做時:
currentKey: localizationPageProxy.vehicleTypes.currentDataKey
您正在創建系結。每當 currentDataKey 的值發生變化時,currentKey 也會隨之更新。但它不是雙向系結。因此更改 currentKey 不會更新 currentDataKey。雙向系結很難實作,但在 SO 上有關于它們的帖子。
要實際解決您要實作的目標,我建議您將 Q_INVOKABLE 函式添加到您的 QObject 呼叫updateCurrentKey或其他內容中。然后在您的 onClicked 處理程式中,執行以下操作:
onClicked: page.model.updateCurrentKey(modelData.key)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448153.html
