我試圖在中繼器的委托中包含一個設定物件,以便我可以保存 SpinBox 的值。但是,我無法弄清楚如何動態設定屬性別名。
我想要實作的是讓屬性別名與modelData 相同。例如,對于 item1: "property alias item1: box.value"; 對于 item2:“屬性別名 item2:box.value”等。
Column {
Repeater {
model: ["item1", "item2", "item3", "item4", "item5"]
delegate: RowLayout {
Settings {
fileName: "config"
category: modelData
property alias value: box.value
}
Label {
text: modelData
}
SpinBox {
id: box
}
}
}
}
上面的代碼生成以下設定,并且是我想要做的事情的解決方法:
[item1]
value=""
[item2]
value=""
[item3]
value=""
...
我想要的是具有值的單個類別,如下所示:
[category]
item1=""
item2=""
item3=""
...
uj5u.com熱心網友回復:
對于這種情況,您不能使用屬性,而只能使用value()
和setValue()
方法:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Qt.labs.settings
ApplicationWindow {
width: 640
height: 480
visible: true
readonly property var values: ["item1", "item2", "item3", "item4", "item5"]
Settings {
id: settings
category: "category"
}
Column {
Repeater {
model: values
delegate: RowLayout {
id: row_layout
Label {
text: modelData
}
SpinBox {
id: box
}
Component.onCompleted: box.value = settings.value(modelData, 0)
Component.onDestruction: settings.setValue(modelData, box.value)
}
}
}
}
輸出:
[category]
item1=6
item2=6
item3=4
item4=2
item5=2
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/361434.html