maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   SailfishOS (https://talk.maemo.org/forumdisplay.php?f=52)
-   -   Colour changing rectangle (https://talk.maemo.org/showthread.php?t=100448)

Markkyboy 2018-08-31 10:16

Colour changing rectangle
 
Hi,

I have a Page, on that page is a rectangle, I can click on the rectangle and it changes colour, I'd like the last colour picked to remain when I reopen the app again. I thought I could do this with settings, but it doesn't work.

It seems as soon as I introduce 'settings' into the code that the app doesn't open. I thought maybe to include 'import com.jolla.settings 1.0' but that doesn't help either, what am I doing wrong?

Code:

    import com.jolla.settings 1.0

        Rectangle {
            id: rect
            anchors.fill: parent
            color: '#000000'
            Settings { //hash this out and the code works but won't save last color created.
                id: settings
                property alias color: rect.color
            }
            MouseArea {
                anchors.fill: parent
                onClicked: rect.color = Qt.hsla(Math.random(), 0.5, 0.5, 1.0);
            }
        }
    }

Thanks,

velox 2018-08-31 13:24

Re: Colour changing rectangle
 
Hi,
have a look at https://sailfishos.org/develop/docs/...ionvalue.html/

cheers

Ancelad 2018-08-31 13:26

Re: Colour changing rectangle
 
Explore Lockscreen Analog Clock settings page.

coderus 2018-08-31 18:51

Re: Colour changing rectangle
 
Explore documentation and stop doing random copy-paste activities without understanding how it works.

Markkyboy 2018-08-31 19:19

Re: Colour changing rectangle
 
Quote:

Originally Posted by coderus (Post 1547865)
Explore documentation and stop doing random copy-paste activities without understanding how it works.

Stop, why?, who are you to tell me to stop?, how about you stop with your narky comments, there are no teachers for this kind of thing and why shouldn't I ask?, this is how we learn.

You don't like my questions?, don't fkin answer them, simples!

Markkyboy 2018-08-31 19:26

Re: Colour changing rectangle
 
Quote:

Originally Posted by velox (Post 1547843)

Thanks velox, I see very little on that page to help me understand how to use it. No examples, not even a slice of code to look at.

Edit: what I mean to say is, I was hoping for more information. However, it's unreal for me, that I didn't connect the dots, just a few days ago Ancelad showed me about ConfigurationGroup, so I do have a starting place. Thanks for your input.

Dave999 2018-08-31 20:14

Re: Colour changing rectangle
 
Quote:

Originally Posted by coderus (Post 1547865)
Explore documentation and stop doing random copy-paste activities without understanding how it works.

Imagine if humans would stop doing things they didn’t understand. :D
Feel free to take it as an advice or...

What documentation would that be?

Markkyboy 2018-08-31 22:38

Re: Colour changing rectangle
 
Quote:

Originally Posted by Dave999 (Post 1547872)
Imagine if humans would stop doing things they didn’t understand. :D
Feel free to take it as an advice or...

What documentation would that be?

Lol, thanks Dave......I have a habit of taking things the wrong way, just as dear coderus can be, well, a bit short at times.

The documentation for sailfish doesn't give much away, fine for those who grew up with coding, got shown, learned from their dad/brother, did it at school, they already have some roots in the subject, I don't have that under my belt.

The documentation as pointed out by velox, I have already looked, it really gives very little information, there is not even an example, which leads me to believe, if you are developing for Sailfish, it is assumed you already know your way round coding. Recently, Ancelad showed me a usage for ConfigurationGroup, okay cool, now I have to learn about dconf and keys, I know how to read/list/search dconf, I even learned how to remove/reset a dconf value but as yet, I'm not quite understanding about 'path' and setting its values, sometimes I need to be shown.

My question about the color changing rectangle was an exercise in understanding Settings, I found it easier work with a rectangle, mouse area and a bit about color, familiar ground, easy stuff. Now I have to connect ConfigGroup to my actual code, which is about changing windspeed units, from default m/s to mph/kmh/kts by tapping on the windspeed output in sailfish weather.

I lack discipline, especially when info appears to be thin on the ground...but the main thing is, I'm keen to learn about it all and I'm not going to stop doing what I'm doing despite what others think, as for reading documentation, I'm always looking at that or my device before asking questions. After all, isn't this what TMO is for?

john_god 2018-09-01 17:39

Re: Colour changing rectangle
 
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

Markkyboy 2018-09-02 08:38

Re: Colour changing rectangle
 
Quote:

Originally Posted by john_god (Post 1547896)
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!


All times are GMT. The time now is 02:46.

vBulletin® Version 3.8.8