0
votes

I'm writing settings with qml Settings

Settings {
    id: powerTuneSettings
    property alias serialPortName: serialName.currentText
}

Saving works but when programm starts the settings gets overwriten by first entry in the model:

ComboBox {
    id: serialName
    width: 200
    model: Serial.portsNames              
}

How can I initialize the combobox with the model and set it to the stored setting?

2

2 Answers

0
votes

Possible solution is to store it as property string and then search for that string in the model in the Component.onCompleted callback of the ComboBox. Set currentIndex if found.

0
votes

The currentText property of Combobox is read-only, which means you cannot directly set it. To chose the current item of your Combobox, you have to set its currentIndex.

Instead of storing currentText in Settings, store the Combobox's currentIndex and it should work as expected.

ComboBox {
    id: serialName
    width: 200
    model: Serial.portsNames
}

Settings
{
    property alias currentIndex: serialName.currentIndex
}

Note that in order for the QML Settings to work, you may have to either set your app's organizationName or Organisation domain in your main.cpp

app.setOrganizationName("yourOrg");
app.setOrganizationDomain("domain.org");