View Single Post
Markkyboy's Avatar
Posts: 433 | Thanked: 727 times | Joined on Oct 2012 @ Costa Blanca, Espaņa
#10
Originally Posted by john_god View Post
Back to your question, Settings belong to Qt.labs.settings wich is not available in Sailfish, so you have to use
ConfigurationGroup, here is a sample program that should work (untested )

Code:
import Nemo.Configuration 1.0

Item {

	Rectangle {
		id: rect
		anchors.fill: parent
		color: '#000000'

		MouseArea {
			anchors.fill: parent
			onClicked: rect.color = Qt.hsla(Math.random(), 0.5, 0.5, 1.0);
		}
	}
	
	
	ConfigurationGroup {
        id: settings
        path: "/apps/harbour-yourApp"
    }
	
	Component.onCompleted: {
		rect.color = settings.value("rectColor", "#000000")
	}
	
	Component.onDestruction: {
		settings.setValue("rectColor", rect.color)
	}
}
From the docs:
variant value(string key, variant defaultValue, int typeHint)
Returns the value of key as a variant. If key does not exist defaultValue will be returned.
The line
rect.color = settings.value("rectColor", "#000000")
tries to load the value of rectColor, if that doenst exist (at the first time it wont) it will load the default value "#000000". The rest is pretty straightforward, on destruction the last rectangle color will be saved in rectColor string. While reading the docs is important, I have to agree that this particular documentation about ConfigurationGroup is very poor.
Feel free to ask any more questions if something is unclear to you, and keep on coding

Cheers
Thank you john_god, just the type of explanation I needed and it with very little tweaking, the code works!

user 'velox' was also on the money, I just needed to see a working example with sailfish to get an understanding of how it works, also Ancelad guided me along a similar path recently using ConfigGroup, but for some reason, I find your example easier to follow/understand and above all, I get the outcome I wanted.

I realised after searching my device, the qt.labs.settings does not exist in sailfish, so one of or at least, my main question should have been "what is the equivalent to qt.labs.settings?".

With minor tweaks, the code looks like this;

Code:
import QtQuick 2.0
import Sailfish.Silica 1.0
import org.nemomobile.configuration 1.0

Page {
    Rectangle {
        id: rect
        anchors.fill: parent
        color: '#000000'

        MouseArea {
            anchors.fill: parent
            onClicked: rect.color = Qt.hsla(Math.random(), 0.5, 0.5, 1.0);
        }
    }
    ConfigurationGroup {
        id: settings
        path: "/apps/harbour-yourApp"
    }
    Component.onCompleted: {
        rect.color = settings.value("rectColor", "#000000")
    }
    Component.onDestruction: {
        settings.setValue("rectColor", rect.color)
    }
the last colour I selected does indeed reappear after closing and opening the application, perfect, just what I was after, kudos john_god!
__________________
..oO(Don't just sit there standing around, pick up a shovel and sweep up!)Oo..
 

The Following 5 Users Say Thank You to Markkyboy For This Useful Post: