0
votes

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
    }
}
2
That probably won't work because the Loader's item is initially null. It becomes non-null after the source has loaded, but that's too late. An alias always needs an object to reference. See the documentation here.JarMan
I was afraid to hear this. My app has a Settings dialog. It has many controls so it makes the app slow. I wanted to use Loader to optimize the app. If it isn't possible I will have to use Local Storage.Edip Ahmet
You could just avoid using an alias, like this: property int myNumber: myloader.item.numberJarMan
When I use int, it isn't saved. Current index is always 0.Edip Ahmet
Ah, I'd never used the Settings object before. I just looked it up and now I understand how it works. You just need a different property to point the alias at. For instance, inside the Loader, add an int property, and then point the alias at that. You'll probably need to initialize the combobox to read from that int too. I'll try it out when I get a chance.JarMan

2 Answers

1
votes

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
    }
}
0
votes

@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.