Reputation: 525
I try to save a variable from another QML object on root QML file using Settings. I can access the variable with myloader.item.number
but when I try it on Settings it gives error: Invalid alias target location: number
Is there a way to save the variable using Settings module?
MyPage.qml
import QtQuick 2.0
import QtQuick.Controls 2.12
Rectangle {
property alias number: combo.currentIndex
color: "red"
ComboBox{
id: combo
model: ["a","b","c"]
}
}
main.qml
import QtQuick 2.3
import QtQuick.Window 2.10
import Qt.labs.settings 1.1
Window{
visible: true
width: 200
height: 200
Component.onCompleted: {
console.log("loader property is:", myloader.item.number)
}
//
Loader{
id:myloader
//asynchronous: true
active: true
source: "MyPage.qml"
anchors.fill: parent
}
/*
// This can save the number with "property alias myNumber: mapage.number"
MyPage{
id: mapage
}
*/
Settings{
id: settings
// This gives error when using loader: Invalid alias target location: number
property alias myNumber: myloader.item.number
}
}
Upvotes: 0
Views: 396
Reputation: 525
@JarMan's answer is better solution. I have found another solution that I also used Settings object to MyPage.qml:
import QtQuick 2.0
import QtQuick.Controls 2.12
import Qt.labs.settings 1.1
Rectangle {
property alias number: settings2.comboIndex
Settings{
id: settings2
property alias comboIndex: combo.currentIndex
}
color: "red"
ComboBox{
id: combo
model: ["a","b","c"]
}
}
main.qml
import QtQuick 2.3
import QtQuick.Window 2.10
import Qt.labs.settings 1.1
Window{
visible: true
width: 200
height: 200
Component.onCompleted: {
console.log("loader property is:", settings.myNumber)
}
Loader{
id:myloader
source: "MyPage.qml"
anchors.fill: parent
}
Settings{
id: settings
property int myNumber: myloader.item.number
}
}
Settings of MyPage holds UI index for ComboBox, Settings of main.qml holds the value of ComboBox.
Upvotes: 0
Reputation: 8277
Since you can't use an alias, you have to manually connect it to Settings. It worked when I did it like this:
import QtQuick 2.3
import QtQuick.Window 2.10
import Qt.labs.settings 1.1
Window{
visible: true
width: 200
height: 200
Component.onCompleted: {
console.log("loader property is:", myloader.item.number)
}
//
Loader{
id:myloader
active: true
sourceComponent: myPage
anchors.fill: parent
Component
{
id: myPage
MyPage
{
// Initialize the combobox from Settings
number: settings.myNumber
// Update the settings from the combobox
onNumberChanged: settings.myNumber = number
}
}
}
Settings{
id: settings
property int myNumber
}
}
Upvotes: 1