Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Colour changing rectangle

    Reply
    Markkyboy | # 1 | 2018-08-31, 10:16 | Report

    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,

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 5 Users Say Thank You to Markkyboy For This Useful Post:
    Amboss, carlosgonz, imaginaryenemy, mosen, santeira

     
    velox | # 2 | 2018-08-31, 13:24 | Report

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

    cheers

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 7 Users Say Thank You to velox For This Useful Post:
    Amboss, carlosgonz, Dave999, imaginaryenemy, Jordi, mosen, santeira

     
    Ancelad | # 3 | 2018-08-31, 13:26 | Report

    Explore Lockscreen Analog Clock settings page.

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 7 Users Say Thank You to Ancelad For This Useful Post:
    Amboss, carlosgonz, Dave999, imaginaryenemy, Jordi, mosen, santeira

     
    coderus | # 4 | 2018-08-31, 18:51 | Report

    Explore documentation and stop doing random copy-paste activities without understanding how it works.

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to coderus For This Useful Post:
    carlosgonz

     
    Markkyboy | # 5 | 2018-08-31, 19:19 | Report

    Originally Posted by coderus View Post
    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!

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 4 Users Say Thank You to Markkyboy For This Useful Post:
    Amboss, carlosgonz, Dave999, santeira

     
    Markkyboy | # 6 | 2018-08-31, 19:26 | Report

    Originally Posted by velox View Post
    Hi,
    have a look at https://sailfishos.org/develop/docs/...ionvalue.html/

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

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by Markkyboy; 2018-08-31 at 21:39.
    The Following 3 Users Say Thank You to Markkyboy For This Useful Post:
    Amboss, carlosgonz, santeira

     
    Dave999 | # 7 | 2018-08-31, 20:14 | Report

    Originally Posted by coderus View Post
    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.
    Feel free to take it as an advice or...

    What documentation would that be?

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by Dave999; 2018-08-31 at 20:21.
    The Following 3 Users Say Thank You to Dave999 For This Useful Post:
    Amboss, carlosgonz, santeira

     
    Markkyboy | # 8 | 2018-08-31, 22:38 | Report

    Originally Posted by Dave999 View Post
    Imagine if humans would stop doing things they didn’t understand.
    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?

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 3 Users Say Thank You to Markkyboy For This Useful Post:
    Amboss, PamNor, santeira

     
    john_god | # 9 | 2018-09-01, 17:39 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 4 Users Say Thank You to john_god For This Useful Post:
    Amboss, Markkyboy, Merienth, peterleinchen

     
    Markkyboy | # 10 | 2018-09-02, 08:38 | Report

    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!

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 5 Users Say Thank You to Markkyboy For This Useful Post:
    Amboss, john_god, Merienth, peterleinchen, velox

     
vBulletin® Version 3.8.8
Normal Logout