Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
1234321
Posts: 21
Registered: ‎10-05-2013
My Device: Z10
My Carrier: ?

Read QSettings as variable name

I am trying to save a variable with QSettings.

If I save it like "app.save("name", variable)" then I end up saving the contents of variable.  This is fine except I may want to change the contents of variable in the future.  If I save it like "app.save("name", "variable")" I save the name of the variable but I can't seem to use it to refer to the variable.  If I try to use it as text in a label:

        Label {
            text: app.read("name")
        }

The text is "variable" not the contents of the original string.

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Read QSettings as variable name

I'm sure this made sense to you when you wrote it but it makes no sense to me?

The examples you have given are how it is expected to work.

If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Read QSettings as variable name

QSettings is a simple key:value storage system no information is saved alongside that info about the way that information was stored or the name of the storage if that's what you are asking.

If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Contributor
1234321
Posts: 21
Registered: ‎10-05-2013
My Device: Z10
My Carrier: ?

Re: Read QSettings as variable name

Sorry.  I'm new to programing.  I didn't mean to say it was not the expected result.  I am wondering what I need to do to use the text of the variable name (restored from QSettings) to refer to the actual variable.

 

Thanks

Please use plain text.
Developer
slashkyle
Posts: 818
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Telus

Re: Read QSettings as variable name

[ Edited ]

you can't do this in most cases because a lot of properties are read only

 

Slider{
id: mySlider value: cpp.getValueFor("myValue","") }

 

instead, when you load your app set this value to a variable and use the variables onChanged function to adjust your content

Container {
   id: root

   property int myInt
   property string myString

onMyIntChanged: {
mySlider.value = myInt
}
Slider {
id: mySlider } onCreationCompleted: {
/* you will need to parseInt() or parseFloat() because
the QSettings is stored as a string */ root.myInt = parseInt(cpp.getValueFor("mySavedInt","")) root.myString = cpp.getValueFor("mySavedString","") } }

 but you'll also need to remeber to update the local value when you save this as well

 

onSomethingHappened: {
    cpp.saveValueFor("mySavedInt",whateverValueIs)
    root.myInt = whateverValueIs
}

 which will in turn update the sliders value

 

This is also useful for accessing variables from other pages, just save their values in QSettings and then you can load them onPopTransitionEnded() 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Read QSettings as variable name

I'm still not getting what you are trying to do but possibly slashkyles answer has answered it anyway.

@slashkyle this is the 2nd or 3rd time I've heard you say use QSettings for passing info between pages. This is terribly inefficient, you are effectively doing multiple file reads and writes each time you navigate between pages. If you took some time to understand about scope, properties, aliases and the signal/slot mechanism you would never need to do this. QSettings should only ever really be used for storing app settings.

If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
slashkyle
Posts: 818
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Telus

Re: Read QSettings as variable name

[ Edited ]

@bbsjdev now that you mention it I think this is probably the 2nd or 3rd time you've said that to me :smileywink: hehe I suppose I'll check into that tomorrow

 

although when using it onPopTransitionEnded() I generally don't write anything, just update the variables like I do onCreationCompleted().  And I sort of don't know if alias would work in my situation as i destroy the pages when they're popped and all the values have already been stored in QSettings so i just call.

 

someProperty = cpp.getValueFor("mySavedValue","")

 I do use property alias to access content from other pages that are custom content containers (my qml example i posted in the c++ thread the other day :smileywink: )

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Read QSettings as variable name

:smileyhappy:@slashkyle I don't really want to hijack this thread but just on your last posting there's no reason that you can't read the data off the page in the onPopTransitionEnded slot as the page object still exists at that time and so you can access the properties within the page. Personally I wouldn't do it this way I would use signals and slots or process the data within the page itself.

 

Let's not add any more irrelevance to this thread if you'd like to discuss it more perhaps you could open a new thread with some of your code where you felt you had to use QSettings and we can then discuss a better way of implementing it.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Contributor
1234321
Posts: 21
Registered: ‎10-05-2013
My Device: Z10
My Carrier: ?

Re: Read QSettings as variable name

I will give you a simple example.

Say I have a button that opens a webpage in the browser.  What webpage it opens is determined with a dropdown menu (with a list of websites).  To make the button work after restarting the app I save the variable that contains the web address (say firstOption, and could contain "http://www.example.com") to QSettings.  Then when the button is pushed it can read the web address from QSettings.

This I know how to do.  The problem is what happens if I want to change one of those sites?  Say I change firstOption to equal "http://m.example.com".  Unless the user reconfigures the button using the dropdown they will contue to go to "http://www.example.com".

 

 

 

Thanks

Please use plain text.
Developer
slashkyle
Posts: 818
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Telus

Re: Read QSettings as variable name

[ Edited ]

When you change whatever is saved in QSettings, update your local variables to reflect that as I demonstrated above

 

you can do that by setting the variable to the value, or just loading the value from qsettings and setting it as the variable.  and then when the variable is changed you can adjust your drop down 

 

Container {
   id: root

property int dropDownIndex

onDropDownIndexChanged: {
//when app is loaded set the last chosen option
myDropDown.selectedIndex = dropDownIndex
}
DropDown{ id: myDropDown Option{ id: option1 value: "http://www.blackberry.com" } Option{ id: option2 value: "http://www.supportforums.blackberry.com" }
onSelectedIndexChanged: {
//when an option is changed it is saved in case the app is closed
cpp.saveValueFor("dropDownIndex",selectedIndex)
} } Button { id: myButton text: "Change Option" //this is if you wanted to keep track of the values onClicked: { //assuming its option1
cpp.saveValueFor("Option1",newValue)

//* new value is whatever you want to set as the new url myDropDown.selectedOption.value = newValue } }
onCreationCompleted: {
root.dropDownIndex = parseInt(cpp.getValueFor("dropDownIndex", ""))
} }

 you have to change them at the same time so that they're in sync :smileywink:

Please use plain text.